问题
Working on currency formatting, I've found an issue when trying to format Chilean pesos.
Following this code:
let priceFormatter = NumberFormatter()
priceFormatter.locale = Locale(identifier: "es_CL")
priceFormatter.numberStyle = .currency
priceFormatter.currencyCode = "CLP"
priceFormatter.string(from: 9990) // A
priceFormatter.string(from: 99900) // B
Executing this I get $9990 for A and $99.990 for B.
What I want to achieve is $9.990 for A
Looks like the formatter is not adding the thousand grouping separator on the first case, which I am not sure why. I have tried adding setting the groupingSize
to 3 without success.
(This only happens with 4 digits)
回答1:
I was trying to achieve the same thing, what I ended up doing was this:
let price: Double = 1000
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .decimal
currencyFormatter.groupingSeparator = "."
currencyFormatter.maximumFractionDigits = 0
let balanceText = currencyFormatter.string(from: NSNumber(value: price))!
return "$" + balanceText
来源:https://stackoverflow.com/questions/58544119/numberformatter-grouping-not-working-as-expected