NumberFormatter grouping not working as expected
在进行货币格式化时,尝试格式化智利比索时发现了一个问题。
以下代码:
1 2 3 4 5 6 7 | 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 |
执行此操作,我得到A的$ 9990和B的$ 99.990。
我想要实现的是A
的$ 9.990
看起来格式化程序没有在第一种情况下添加千位分组分隔符,我不确定为什么。我尝试将设置
(仅发生4位数字)
我遇到了同样的问题,并在这里找到了解释:
iOS13:尝试使用es_ES语言环境
格式化4位数字时,NumberFormatter缺少groupingSeparator
我最终设置了us_US语言环境:
1 2 3 4 5 | let formatter = NumberFormatter() formatter.locale = Locale(identifier:"us_US") formatter.groupingSeparator ="" formatter.usesGroupingSeparator = true formatter.groupingSize = 3 |
我试图实现相同的目标,但最终我要做的是:
1 2 3 4 5 6 7 8 | 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 |