下面是有问题的代码行:
let finalNumber = String(format: "%.2f", anotherNumber)
// where anotherNumber is a decimal like 123.456789我得到的输出是123.45。我预计这个数字会四舍五入到123.46。
有人能告诉我为什么它不能正确地取整吗?
发布于 2018-12-11 14:37:54
在游乐场中尝试以下功能
func roundToDecimal(_ fractionDigits: Int, value: Double) -> Double {
let multiplier = pow(10, Double(fractionDigits))
return Darwin.round(value * multiplier) / multiplier
}
roundToDecimal(2, value: 123.456789)你会得到你的结果123.46
https://stackoverflow.com/questions/53715293
复制相似问题