js Math.round() Math.floor() Math.ceil()
Math.floor() ,Math.round()方法执行的是取整计算,它们得到的是与之最接近的整数当参数没有的时候,返回你NaNMath.floor()NaNMath.round()NaN
我们再使用Math.floor()传入其它参数看看,如下所示。可以看出返回的都是比它小的,最接近它的整数Math.floor(2.9)2Math.floor(-2.9)-3Math.floor(-0.5)-1Math.floor(-0.9)-1Math.floor(0.9)0Math.floor(1.9)1
Math.round() 方法可把一个数字四舍五入为最接近的数。可以看出Math.round(),的使用和我们平时使用的四舍五入类似,它返回的是最接近它的整数Math.round(1.2)1Math.round(1.6)2Math.round(-1.6)-2Math.round(0.9)1Math.round(0.49)0Math.round(0.01)0Math.round(-0.01)-0Math.round(-0.4)-0
ceil() 方法是向上取整计算,它返回的是大于或等于参数的整数数据同样Math.ceil()没传入参数也是返回NaNMath.ceil()NaN
ceil() 方法是向上取整计算,它和floor其实刚好相反,下面我们传入一些参数来看看该方法,可以看出返回的都是比它大的,最接近它的整数Math.ceil()NaNMath.ceil(1.2)2Math.ceil(-1.2)-1Math.ceil(22.10)23Math.ceil(77.10)78Math.ceil(76.9999)77Math.ceil(-76.9999)-76
注意 Math.floor()和 Math.ceil()的区别