userA在法国,他们将某物的价格设置为10欧元,我得到了他们的货币信息,并使用Locale.current.identifier
将他们的本地区域记录到了数据库中,这给了我€
,我将其保存到dataModel.localeIdentifier
// when userA uploads
let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.numberStyle = .currency
formatter.locale = Locale.current
formatter.numberStyle = NumberFormatter.Style.currency
// save their Locale.current.identifier to the db
dataModel.localeIdentifier = Locale.current.identifier
userB在英国,他们看到了userA发布的内容,也许他们想以€20的价格购买其中的两个,这意味着我必须删除欧元符号,进行一些计算,然后稍后再添加欧元符号。我只是想使用userA中的dataModel.localeIdentifier
并将其设置为userB使用的格式化程序,而不是手动将其附加回去。这样,欧元符号将出现在新的金额20上
我不想使用userB的Locale.current.identifier来显示英镑,而是希望它显示欧元符号。我试过了,但是没用
// userB
let amount = updatedAmount(amount: 20) // strips away the € sign and just uses the Double
let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.numberStyle = .currency
// identifierFromUserA is €
let identifierFromUserA = dataModel.localeIdentifier // this isn't nil because I'm force unwrapping it below
let localeFromUserA = Locale(identifier: identifierFromUserA!)
// *** after setting userB's formatter.locale to the one from localeFromUserA I'm still getting the local currency instead of the € ***
formatter.locale = localeFromUserA
formatter.numberStyle = NumberFormatter.Style.currency
// this should now show €20
let newAmount = formatter.string(from: NSNumber(value: amount))
问题是在将userB的formatter.locale设置为localeFromUserA之后,我仍在获取userB的本地货币而不是€
答案 0 :(得分:0)
这是一个编码错误,我发现了错误,在设置userA的dataModel.localeIdentifier = Locale.current.identifier
时,我有另一个函数后来将其更改为dataModel.localeIdentifier = Locale.current.currencySymbol
,这就是为什么我得到欧元符号€
的原因而不是en_FR