如果您有这样的代码,则 p
将为 "2.99"
:
let price = 2.99
let p = String(format: "%.2f", price)
但是,如果您有这样的代码:
let priceNS: NSDecimalNumber = 2.99
let p2 = String(format: "%.2f", priceNS)
那么p2
就是"0.00"
。
如何将 NSDecimalNumber
格式化为这样的字符串? (NSDecimalNumber
是 SKProduct
中价格的存储方式)
最佳答案
您应该使用 NumberFormatter 格式化您的产品价格并使用您的产品区域设置:https://developer.apple.com/documentation/storekit/skproduct/1506094-price
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = someSKProduct.priceLocale
let formattedPrice = numberFormatter.string(from: someSKProduct.price) ?? ""
关于ios - 如何将 SKProduct 价格格式化为 Swift 货币字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46554294/