Ios 如何使用带小数的currencyFormatter
我试图获取存储在CoreData中的十进制数,并通过Swift 3中的货币格式化程序运行它。以下是我试图使用的:Ios 如何使用带小数的currencyFormatter,ios,xcode,core-data,swift3,currency-formatting,Ios,Xcode,Core Data,Swift3,Currency Formatting,我试图获取存储在CoreData中的十进制数,并通过Swift 3中的货币格式化程序运行它。以下是我试图使用的: var currencyFormatter = NumberFormatter() currencyFormatter.usesGroupingSeparator = true currencyFormatter.numberStyle = NumberFormatter.Style.currency // localize to your grouping and decimal
var currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = NumberFormatter.Style.currency
// localize to your grouping and decimal separator
currencyFormatter.locale = NSLocale.current
var priceString = currencyFormatter.stringFromNumber(NSNumber(totalAmount))
其中totalAmount是我用于CoreData的十进制数
但是。我在尝试将十进制数转换为数字时出现此错误
参数标签'.''与任何可用的重载都不匹配您可以有如下内容:
class YourClass: UIViewController {
static let priceFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.numberStyle = .currency
return formatter
}()
}
用法:
yourLabel.text = YourClass.priceFormatter.string(from: totalAmount)
stringFromNumber已重命名为stringfrom:,例如
但您不必转换为NSNumber
使用stringfrom:或stringfor:
var priceString = currencyFormatter.string(from: NSNumber(totalAmount))
var priceString = currencyFormatter.string(for: totalAmount)