【问题标题】:Why can't round correctly using Math.round?为什么不能使用 Math.round 正确舍入?
【发布时间】:2021-07-13 10:29:22
【问题描述】:

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

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

调试器说:

iDouble = 118.64300000000001
solution = 118.6

【问题讨论】:

  • 输出正确。预期的结果是什么?

标签: android kotlin rounding


【解决方案1】:

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

118.64300000000001d * 10L = 1186.43d

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

Math.round(1186.43d) = 1186L.

1186L / 10.00d = 118.6d

所以我觉得很好。请记住,除以 double/float 将导致 double/float。

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多