【发布时间】:2018-10-10 10:10:22
【问题描述】:
我在 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)。
有什么办法解决吗?
另一个我很满意的解决方案:以美元显示每个人的价格,无论用户的语言和货币如何。
【问题讨论】:
-
商品的价格(例如价值)基于您在 iTunes 中登录的国家/地区,而不是地区。因此,如果您使用美国帐户在 iTunes(沙盒?)中设置,您将看到美国值
-
我认为最好的选择是显示
priceLocale.currencySymbol。该符号在某些情况下会放错位置,但至少它是具有良好货币的良好价格。
标签: ios swift nsnumberformatter currency-formatting skproduct