我在iOS应用程序中的应用程序采购中使用。根据用户/设备的不同,我想以正确的格式显示价格。
这是我的密码:
let price=product.price
let numberFormatter = NumberFormatter()
numberFoxrmatter.formatterBehavior = .behavior10_4 //doesn't change anything if I remove this line
numberFormatter.numberStyle = .currency
numberFormatter.locale = product.priceLocale
let formattedPrice=numberFormatter.string(from: price)但在某些情况下,货币符号并不是好的符号和/或放错了地方。在我的例子中,价格产品是19.99美元或20,99欧元。
示例
来自设备的:
product.priceLocale:en_FR@currency=EUR (fixed)
Locale.current:en_FR (current)
产出:20,99欧元
应显示:20,99欧元
来自模拟器的:
product.priceLocale:en_FR@currency=EUR (fixed)
Locale.current:en_US (current)
产出:20.99美元
应显示:20,99欧元或19.99美元
我有几个用户,他们与其他货币有相同的问题,符号应该放在价格后,不像美元格式。另一个看到$7290而不是7290₸的用户(这是一个完全不同的价格.)。
我很确定这与语言设置或Locale.current有关。但是,如果我在我的设备上将我的主要语言改为法语,我的价格与“20,99欧元”相同。奇怪的是我的Locale.current切换到了en_US (current)。
有办法解决这个问题吗?
另一个我会满意的解决方案是:以美元显示每个人的价格--,不管用户的语言和货币如何。
发布于 2018-10-10 10:15:20
尝尝这个
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
// localize to your grouping and decimal separator
currencyFormatter.locale = Locale.current
// We'll force unwrap with the !, if you've got defined data you may need more error checking
let priceString = currencyFormatter.string(from: 9999.99)!
print(priceString) // Displays $9,999.99 in the US locale示例
currencyFormatter.locale = Locale(identifier: "fr_FR")
if let priceString = currencyFormatter.string(from: 9999.99) {
print(priceString) // Displays 9 999,99 € in the French locale
}发布于 2018-10-10 13:13:00
区域设置是正确输出的关键。en_FR读起来像英语和法语地区。这将导致使用法语价格为-> 10欧元的英语国家的格式化输出。
使用模拟器并将region & language设置为法语并使用Locale.current。它应该读取fr_FR并给出正确的输出。10,00欧元
您是否试图在模拟器上更改语言和区域,并对priceLocale产生影响?
https://stackoverflow.com/questions/52737762
复制相似问题