Math 类中提供了三个与取整相关的方法:ceil,floor,round
这三个方法的作用与其英文解释相对应,如下:
ceil 表示向上取整 : 所以 Math.ceil(2.2) = 3 Math.ceil(-2.2) = -2
floor 表示向下取整 : 所以 Math.floor(2.2) = 2 Math.floor(-2.2) = -3
round 方法表示四舍五入,算法为Math.floor(x + 0.5),
所以Math.round(2.2) = Math.floor(2.2+0.5) = 2
Math.round(-2.2) = Math.floor(-2.2+0.5) = -2