首页 > 解决方案 > 为什么不能使用 Math.round 正确舍入?

问题描述

为什么我不能正确四舍五入:

val solution = Math. round(iDouble * 10.00) / 10.00

调试器说:

iDouble = 118.64300000000001
solution = 118.6

标签: androidkotlinrounding

解决方案


我看不出有什么问题。这是解释。

118.64300000000001d * 10L = 1186.43d

Math.round 将舍入为整数部分的数字作为长整数返回。

数学.round(1186.43d) = 1186L。

1186L / 10.00d = 118.6d

所以我觉得很好。请记住,除以双精度/浮点数将导致双精度/浮点数。


推荐阅读