【问题标题】:How to use currency values in a calculation如何在计算中使用货币值
【发布时间】:2014-07-28 20:28:24
【问题描述】:

我有一个变量来存储我需要在计算中使用的货币值,但是我不知道如何在计算中使用它,因为变量的开头有一个货币符号(例如 £ 2.43,70.82 美元,300.21 欧元)。我曾考虑用任何内容替换第一个字符,但这不起作用,因为有些货币使用多个字符,有些货币在值后面有符号。

有谁知道我如何删除除值本身之外的所有字符?或者另一种将变量纳入计算的方法? (顺便说一句,我使用的是 Swift)

编辑:

我一直在尝试 NSDecimalNumber 来转换字符串,但是我收到错误消息“NSNumber 无法转换为 NSDecimalNumber”。这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    resetButtonNav.enabled = false

    self.navigationController.navigationBar.barTintColor = UIColor.newBlueColor()
    self.tabBarController.tabBar.selectedImageTintColor = UIColor.newBlueColor()
    UINavigationBar.appearance().barTintColor = UIColor.newBlueColor()
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()

    textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    self.view.addSubview(textField)

    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currencyFormatter.currencyCode = NSLocale.currentLocale().displayNameForKey(NSLocaleCurrencySymbol, value: NSLocaleCurrencyCode)
}

func textFieldDidChange(textField: UITextField) {
    var text = textField.text.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
    textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
}

@IBAction func enterBudgetButton(sender : AnyObject) {
    finalUserBudget = textField.text
    budgetName = budgetNameText.text

    var currencyFormatterTest:NSNumberFormatter
    currencyFormatterTest.formatterBehavior = NSNumberFormatterBehavior.Behavior10_4
    currencyFormatterTest.generatesDecimalNumbers = true
    currencyFormatterTest.numberStyle = NSNumberFormatterStyle.CurrencyStyle

    var finalUserBudgetFormatted: NSDecimalNumber = currencyFormatter.numberFromString(finalUserBudget)
    }

【问题讨论】:

  • 你想用NSNumberFormatter把字符串变成NS(Decimal)Number
  • 啊。我已经更新了我的问题。

标签: swift


【解决方案1】:

您可以使用问题中的此代码:

var text = textField.text.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)

做这个:

func currencyStringAsDouble(currencyString: String) -> Double {
    let cleanedString = currencyString.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
    let currencyDouble = (text as NSString).doubleValue / 100.0
    return currencyDouble;
}

【讨论】:

  • 对不起,您是建议我在使用其他功能的同时使用该功能吗?
  • 视情况而定。你如何存储你的价值观?您是否将价格对象存储为字符串?如果是这样,您可以使用它来将它们转换回双打。我建议将它们存储为双精度以避免将来出现问题,在这种情况下,这可以用来代替您当前的实现以及当您想要将某些文本转换为双精度以进行存储时。
  • 啊,太完美了!我决定像你建议的那样将它们存储为双打,而且效果很好。再次感谢!
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
相关资源
最近更新 更多