Math.ceil 向上取整
Math.floor 向下取整
Math.round 四舍五入取整
Math.round()函数返回一个数字四舍五入后最接近的整数
如果参数的小数部分大于0.5,四舍五入到相邻的绝对值更大的整数
如果参数的小数部分小于0.5,四舍五入到相邻的绝对值更小的整数
如果参数的小数部分等于0.5,四舍五入到相邻的在正无穷(+∞)方向上的整数。
例:
x=Math.round(2019.49) ; //2019
x=Math.round(2019.5); //2020
x=Math.round(-2019.5); //-2019
x=Math.round(-2019.51); //-2020
round()方法可以这样理解:
将括号内的数+0.5之后,向下取值,
比如:round(3.4)就是3.4+0.5=3.9,向下取值是3,所以round(3.4)=3;
round(-10.5)就是-10.5+0.5=-10,向下取值就是-10,所以round(-10.5)=-10
所以,Math.round(11.5)=12;
所以,Math.round(-11.5)=-11;