Swift Double精度丢失的问题
2023-03-22 本文已影响0人
CYC666
// %.10f 情况下,2376770.3800000000 会变成 2376770.3799999999
let text2 = String.init(format: "%.10f", text1.doubleValue)
let value1 = 0.1 + 0.2 // 应该等于0.3
let text1 = String(value1)
print(text1) // 打印值:0.30000000000000004
let text2 = String.init(format: "%.10f", value1) // 打印值:0.3000000000
print(text2)
let text3 = String.init(format: "%.17f", value1) // 打印值:0.30000000000000004
print(text3)
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 10
let text4 = formatter.string(for: value1)
print(text4!) // 打印值:0.3