NumberFormatter grouping not working as expected

£可爱£侵袭症+ 提交于 2019-12-24 21:11:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!