1

在Math类的静态方法round()中,我注意到一件令我难以理解的事情:

Math.round(0.4999999999999999);  // is 0
Math.round(0.49999999999999999); // is 1

为什么?

4

1 回答 1

14

0.49999999999999999有效数字太多,一个double变量不能全部存储。所以隐式舍入发生在编译期间。当您调用Math.round()时,参数已经是 0.5(检查自己:0.49999999999999999 == 0.5yield true)。

于 2013-02-16T18:58:41.410 回答