先转一篇文章,这篇文章讲得非常全面和详细,值得参考!
文章源地址:http://tech.e800.com.cn/articles/2009/85/1249435622799_1.html
舍掉小数取整:Math.floor(2.0)=2
舍掉小数取整:Math.floor(2.1)=2
舍掉小数取整:Math.floor(2.5)=2
舍掉小数取整:Math.floor(2.9)=2
舍掉小数取整:Math.floor(-2.0)=-2
舍掉小数取整:Math.floor(-2.1)=-3
舍掉小数取整:Math.floor(-2.5)=-3
舍掉小数取整:Math.floor(-2.9)=-3
四舍五入取整:Math.rint(2.0)=2
四舍五入取整:Math.rint(2.1)=2
四舍五入取整:Math.rint(2.5)=2
四舍五入取整:Math.rint(2.9)=3
四舍五入取整:Math.rint(-2.0)=-2
四舍五入取整:Math.rint(-2.1)=-2
四舍五入取整:Math.rint(-2.5)=-2
四舍五入取整:Math.rint(-2.9)=-3
凑整:Math.ceil(2.0)=2
凑整:Math.ceil(2.1)=3
凑整:Math.ceil(2.5)=3
凑整:Math.ceil(2.9)=3
凑整:Math.ceil(-2.0)=-2
凑整:Math.ceil(-2.1)=-2
凑整:Math.ceil(-2.5)=-2
凑整:Math.ceil(-2.9)=-2
四舍五入取整:Math.round(2.0)=2
四舍五入取整:Math.round(2.1)=2
四舍五入取整:Math.round(2.5)=3
四舍五入取整:Math.round(2.9)=3
四舍五入取整:Math.round(-2.0)=-2
四舍五入取整:Math.round(-2.1)=-2
四舍五入取整:Math.round(-2.5)=-2
四舍五入取整:Math.round(-2.9)=-3
我今天被Math.floor阴到的是,它对负数的取整。以前用过Math.floor取整,所以今天需要时也就随手一写。但是到后面跑的时候,发现在出现对负数处理的情况时,计算的结果总是会有所偏颇。在后台打出log看,发现对负数的处理总是出现莫名其妙的错误,都会有点小问题。看了好几遍代码,还是感觉没问题,如果有问题就只能是Math.floor的问题。于是看了下API,发现API写着:
Returns the largest (closest to positive infinity)
|