ios - Localized currency symbol from NumberFormatter
问题描述
how to set NumberFormatter to return currency symbol not the iso one but local which is visible in iOS locale settings pane. Like for Polish currency I need to have “zł” symbol not the “PLN”.
I cannot find any way to get it from system as this cannot be hardcoded. IAP localized price also uses “zł” not “PLN”</p>
I tried this way:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencySymbol = Locale.current.currencySymbol
formatter.maximumFractionDigits = 2
let price = formatter.string(from: (offeringPrice / 12) as NSNumber) ?? ""
but whatever I try to use as currency symbol I always get in return "PLN"
解决方案
如果您设置formatter.locale
为pl_PL
有效:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2
formatter.currencyCode = "PLN"
let polandLocale = Locale(identifier: "pl_PL")
let usLocale = Locale(identifier: "en_US")
let offeringPrice = 50.0
let price = (offeringPrice / 12) as NSNumber
formatter.locale = usLocale
formatter.string(from: price) // "PLN 4.17"
formatter.locale = polandLocale
formatter.string(from: price) // "4,17 zł"
推荐阅读
- reactjs - 无效的钩子调用错误,但一切似乎都很好
- reactjs - 我们如何在 react-native 中将崩溃错误发送到自己的服务器
- javascript - 如何在 react-native 中清除 TextInput 值?
- python - 用字符串格式和 nan 绘制 pandas 数据框中所有列的直方图
- apache-kafka - 来自 Stream 的 ksqlDB 主题仅在前几次返回结果
- scala - 尝试实现“Absurd”类型类时出现隐式错误
- java - XML 不针对多个 XSD 进行验证仅在 fGrammarDescription 中加载一个文件
- blender - 为什么我的渲染没有使用 CUDA,即使我已选择它并将其保存在我的首选项中
- .net - 如何在 C# 中使用 iText7 将 DataGridView 导出为 PDF(注意:不使用 iTextSharp)?
- aws-lambda - 如何使用 API Gateway websocket 构建 graphql lambda?