我有一本字典,其中 Bill Price 作为 Optional Any 我的问题是我想使用以下函数将其转换为货币:
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale.current
let priceString = currencyFormatter.string(from: ToConvert2)
print(priceString) // Displays $9,999.99 in the US locale
我使用以下数据:
dicType.value(forKey: "BON_PRIX")
-> 可选 - 一些:103.28
我试过了:
let ToConvert = (String(describing: dicType.value(forKey: "BON_PRIX") as! String))
let ToConvert2 = NSNumber(value: Int(ToConvert)!)
但我遇到了致命错误,
在展开可选值时意外发现 nil
我尝试了几件事,但没有找到正确的方法。因此,关键是将来自外部服务器的数据转换为欧元,并带有 2 位小数。
在此先感谢您的帮助!