首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迅速显示SKProduct错误价格

迅速显示SKProduct错误价格
EN

Stack Overflow用户
提问于 2018-10-10 10:10:22
回答 2查看 2.6K关注 0票数 1

我在iOS应用程序中的应用程序采购中使用。根据用户/设备的不同,我想以正确的格式显示价格。

这是我的密码:

代码语言:javascript
复制
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.priceLocaleen_FR@currency=EUR (fixed)

Locale.currenten_FR (current)

产出:20,99欧元

应显示:20,99欧元

来自模拟器的

product.priceLocaleen_FR@currency=EUR (fixed)

Locale.currenten_US (current)

产出:20.99美元

应显示:20,99欧元或19.99美元

我有几个用户,他们与其他货币有相同的问题,符号应该放在价格后,不像美元格式。另一个看到$7290而不是7290₸的用户(这是一个完全不同的价格.)。

我很确定这与语言设置Locale.current有关。但是,如果我在我的设备上将我的主要语言改为法语,我的价格与“20,99欧元”相同。奇怪的是我的Locale.current切换到了en_US (current)

有办法解决这个问题吗?

另一个我会满意的解决方案是:以美元显示每个人的价格--,不管用户的语言和货币如何。

EN

回答 2

Stack Overflow用户

发布于 2018-10-10 10:15:20

尝尝这个

代码语言:javascript
复制
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

示例

代码语言:javascript
复制
currencyFormatter.locale = Locale(identifier: "fr_FR")
if let priceString = currencyFormatter.string(from: 9999.99) {
    print(priceString) // Displays 9 999,99 € in the French locale
}

有关更多细节,请查看https://supereasyapps.com/blog/2016/2/8/how-to-use-nsnumberformatter-in-swift-to-make-currency-numbers-easy-to-read

票数 1
EN

Stack Overflow用户

发布于 2018-10-10 13:13:00

区域设置是正确输出的关键。en_FR读起来像英语和法语地区。这将导致使用法语价格为-> 10欧元的英语国家的格式化输出。

使用模拟器并将region & language设置为法语并使用Locale.current。它应该读取fr_FR并给出正确的输出。10,00欧元

您是否试图在模拟器上更改语言和区域,并对priceLocale产生影响?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52737762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档