我有一个问题,我需要有不同精度的结果,最多一个在昏迷后最多18位。
因为此操作包含金钱,所以我决定使用Decimal
类型。所以我有疑问。
Decimal
数据类型是否适合表示最多18个值
昏迷后的数字?
我会在iPhone 5等旧手机上有不同的行为吗? 使用该数据类型?
使用Decimal
和NumberFormatter()
maximumFractionDigits
= 18时,是否存在任何极端情况? (错误的四舍五入等)
这是我的例子用例:
我有美元硬币的价格:
let pricePerCoin: Decimal = 950.83
我将用美元金额购买硬币。
let amountInUSD: Decimal = 10000
我使用formatter能够将Decimal
数字转换为maximumFractionDigits
的字符串。
let fractionDigits = 18
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ""
formatter.isLenient = true
formatter.maximumFractionDigits = fractionDigits
结果
let result = formatter.string(for: amountInUSD/pricePerCoin) // 10.517127141550011933