Math.ceil()
ceil的意思就是天花板,Math.ceil()方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。
Math.ceil(-1.1); // -1
Math.ceil(-1.5); // -1
Math.ceil(-1.8); // -1
Math.ceil(0.1); // 1
Math.ceil(0.5); // 1
Math.ceil(0.8); // 1
Math.ceil(1.1); // 2
Math.ceil(1.5); // 2
Math.ceil(1.8); // 2
Math.floor()
floor的意思就是地板,floor() 方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。
Math.floor(-1.1); // -2
Math.floor(-1.5); // -2
Math.floor(-1.8); // -2
Math.floor(0.1); // 0
Math.floor(0.5); // 0
Math.floor(0.8); // 0
Math.floor(1.1); // 1
Math.floor(1.5); // 1
Math.floor(1.8); // 1
Math.round()
round() 方法可把一个数字舍入为最接近的整数。它的原理相当于Math.floor(x+0.5)。
Math.round(-1.1); // -1
Math.round(-1.5); // -1 尤其注意这个
Math.round(-1.8); // -2
Math.round(0.1); // 0
Math.round(0.5); // 1
Math.round(0.8); // 1
Math.round(1.1); // 1
Math.round(1.5); // 2
Math.round(1.8); // 2