NumberFormatter分组未按预期工作 [英] NumberFormatter grouping not working as expected
本文介绍了NumberFormatter分组未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在进行货币格式设置时,我尝试格式化智利比索时发现了一个问题.
Working on currency formatting, I've found an issue when trying to format Chilean pesos.
以下代码:
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
Executing this I get $9990 for A and $99.990 for B.
What I want to achieve is $9.990 for A
好像格式化程序没有在第一种情况下添加千位分组分隔符,我不知道为什么.我尝试将groupingSize
设置为3,但没有成功.
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.
(这仅发生在4位数字上)
(This only happens with 4 digits)
推荐答案
我试图实现相同的目标,最终我要做的是:
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
这篇关于NumberFormatter分组未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文