gpt4 book ai didi

ios - NumberFormatter 将百分比值加倍得到 nil

转载 作者:搜寻专家 更新时间:2023-11-01 06:03:23 25 4
gpt4 key购买 nike

我想通过以下方式将 String precent 值转换为 Double:

        let formatter = NumberFormatter()
formatter.numberStyle = .percent
let a = formatter.number(from: "12.5%")
print(a)

它在 Playground 上运行良好。但是在“romana”(Română)语言下的项目中测试时,它会打印nil。如果我将 12.5% 更改为 int 类型的百分比值,如 56%,则没有问题。

有什么想法吗?

附上语言设置页面。 enter image description here

最佳答案

If i change 12.5% to int type percentage value like 56% it works no problem.

这通常意味着区域设置使用不同的字符作为小数点分隔符,即逗号 , 而不是点

切换到 12,5% 应该可以解决这个问题。

I got all the source number with . separator

如果您的号码在源代码中是硬编码的,则不应依赖用户选择的区域设置。使用 system locale用于处理硬编码输入:

formatter.locale = Locale.system

[Locale.system is] The generic locale that contains fixed “backstop” settings that provide values for otherwise undefined keys. Use the system locale when you don’t want any localizations.

关于ios - NumberFormatter 将百分比值加倍得到 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com