NSNumberFormatter格式货币不起作用

时间:2015-10-15 15:25:36

标签: ios string swift swift2

我有一个数字格式化程序:

let currencyFormatter:NSNumberFormatter = NSNumberFormatter()
        currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        currencyFormatter.currencyCode = "$"

将其设置为标签:

cell.ValueLabel.text = currencyFormatter.stringFromNumber(product.price)

获取100.00而不是100.00$

1 个答案:

答案 0 :(得分:2)

您必须设置currencySymbol而不是currencyCode

let currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = .CurrencyStyle
currencyFormatter.currencySymbol = "$"
currencyFormatter.stringFromNumber(100)  // $100.00"

否则

let currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = .CurrencyStyle
currencyFormatter.currencyCode = "USD"
currencyFormatter.stringFromNumber(100)  // $100.00"