尝试四舍五入 17.775
Try to round 17.775
我几乎尝试了所有四舍五入小数的方法
none 他们在工作
(round(100 * 17.775)/100)
我得到的结果是 17.77
由于 round
,我需要 17.78
请提供解决方案。
*注意:这个问题只适用于 17.775 意味着解决方案不适用于小数点后三位和最后的 5 [以 5 结尾只会产生问题] *
提前致谢
这是检查最后一个数字是否为 5 或小于 5 并相应地执行操作的解决方案
let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
let getLastNumberMultiplier = pow(10.0, numberOfPlaces+1)
var num = 17.775
let lastNumber = Int(num * getLastNumberMultiplier) % 10
if lastNumber >= 5 {
num = round(((num * multiplier ) + 1 ) ) / multiplier
}
else {
num = round(num * multiplier) / multiplier
}
print(num)
伙计们,我尝试了很多答案,最后我从我的一位朋友那里得到了解决方案
将 'round' 替换为 'roundf' 解决了问题
let neededAnswer = Double((roundf(100 * Float(db))/100))
这使得 17.775 变为 17.78
并使 17.774 变为 17.77
我想要这个,这是最终答案。
感谢你们的所有努力,如果您发现此答案有任何问题,请添加更多 answers/solutions 并纠正我。
我几乎尝试了所有四舍五入小数的方法 none 他们在工作
(round(100 * 17.775)/100)
我得到的结果是 17.77 由于 round
,我需要 17.78请提供解决方案。
*注意:这个问题只适用于 17.775 意味着解决方案不适用于小数点后三位和最后的 5 [以 5 结尾只会产生问题] * 提前致谢
这是检查最后一个数字是否为 5 或小于 5 并相应地执行操作的解决方案
let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
let getLastNumberMultiplier = pow(10.0, numberOfPlaces+1)
var num = 17.775
let lastNumber = Int(num * getLastNumberMultiplier) % 10
if lastNumber >= 5 {
num = round(((num * multiplier ) + 1 ) ) / multiplier
}
else {
num = round(num * multiplier) / multiplier
}
print(num)
伙计们,我尝试了很多答案,最后我从我的一位朋友那里得到了解决方案
将 'round' 替换为 'roundf' 解决了问题
let neededAnswer = Double((roundf(100 * Float(db))/100))
这使得 17.775 变为 17.78 并使 17.774 变为 17.77
我想要这个,这是最终答案。
感谢你们的所有努力,如果您发现此答案有任何问题,请添加更多 answers/solutions 并纠正我。