为什么不能使用 Math.round 正确舍入?

Why can't round correctly using Math.round?

为什么我不能正确舍入:

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

调试器说:

iDouble = 118.64300000000001
solution = 118.6

我不明白这有什么问题。这是解释。

118.64300000000001d * 10L = 1186.43d

Math.round 将 return 数字舍入到整数部分作为 long。

Math.round(1186.43d) = 1186L.

1186L / 10.00d = 118.6d

所以我觉得还不错。请记住,除以 double/float 将导致 double/float.