iOS13:尝试使用 es_ES 区域设置格式化 4 位数字时,NumberFormatter 缺少 groupingSeparator

iOS13: NumberFormatter missing groupingSeparator when try to format 4 digits numbers with es_ES locale

我已将我的项目更新为 iOS 13,我发现我用于格式化价格的价格格式化程序已停止正常工作。

groupingSeparator,将千位分组的符号,缺少 4 位数字:使用西班牙语语言环境时从 1000 到 9999 es_ES

下面有一个简单的片段来验证它。 NumberFormatter 似乎对 en_US 语言环境和高于 9999 的数字工作得很好,但每当使用 es_ES 语言环境并尝试格式化 4 位数字时,groupingSeparator(千位分隔符) 丢失了,所以我得到的不是 1.000,而是 1000。


let enFormatter = NumberFormatter()
enFormatter.locale = Locale(identifier: "en_US")
enFormatter.numberStyle = .decimal
enFormatter.string(from: 1000)
enFormatter.string(from: 9999)
enFormatter.string(from: 10000)
enFormatter.string(from: 100000)

let esFormatter = NumberFormatter()
esFormatter.locale = Locale(identifier: "es_ES")
esFormatter.numberStyle = .decimal
esFormatter.string(from: 1000)
esFormatter.string(from: 9999)
esFormatter.string(from: 10000)
esFormatter.string(from: 100000)

只需将其粘贴到 playground 中并检查输出即可。

我是不是漏掉了什么?

到目前为止,我已将问题报告给 Apple:FB7416623 https://feedbackassistant.apple.com/feedback/7416623(不确定 link 是否有效)

我会保持更新,但如果有人能提供更多相关信息,我将不胜感激。

基于 this article from the RAE (Real Academia de la Lengua Española: Royal Academy of the Spanish Language), and this other one from the Fundéu(Fundación del Español Urgente:紧急西班牙基金会),现在的规则是不对 4 位数字使用分组分隔符,并使用白色 space 作为其余情况的分组分隔符。例如:

1000  -> 1000
15000 -> 15 000

但是 ISO 规则建议即使是 4 位数字也应保留分组分隔符。

I don’t recommend using your method. What you should do is return a function for the text;

function addComma(number) local left, num, right = string.match(number, '^([^%d]%d)(%d)(.-)$') return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right end All you have to do is call the function, and the function returns the text with commas. I use this for any game I have that I need currency displayed. Hope this helped!

Also please note, this doesn’t add M+ , K+ , this adds a comma like it does in the Leaderboard. If you wish to know how to add M and K’s to your text here is a video that can explain it well for you;

https://www.youtube.com/watch?v=Mov5znC3yOA

What you do with that is just change what he does for the leaderboard and do it in the text.

使用AddComma函数来解决这个问题。正如 Kensizo 所说,您所要做的就是调用函数

https://devforum.roblox.com/t/custom-coin-gui-bug/345991/2