Math.round(-1.5) = -1
Math.round(1.5) = 2
JDK 中的 java.lang.Math 类提供了三个与取整有关的方法:ceil、floor、round
(1)ceil:向上取整
返回小数所在两整数间的较大值,如 -1.5 返回 -1。
Math.ceil(11.3) = 12
Math.ceil(-11.3) = -11
(2)floor:向下取整
返回小数所在两整数间的较小值,如 -1.5 返回 -2。
Math.floor(11.3) = 11
Math.floor(-11.3) = -12
(3)round:四舍五入
四舍五入其实是个假名,应该是加0.5然后向下取整。
负 .5 小数返回较大整数,如 -1.5 返回 -1。所以负数不应该是四舍五入。
Math.round(11.3) = 11
Math.round(11.8) = 12
Math.round(-11.3) = -11
Math.round(-11.8) = -12