码迷,mamicode.com
首页 > Web开发 > 详细

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

时间:2018-02-07 00:39:31      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:简单   返回   解析   最小   字符串   logs   等于   tle   title   

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

Math.round、parseInt、Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结。

一、Math.round

作用:四舍五入,返回参数+0.5后,向下取整。

如:

Math.round(5.57)  //返回6

Math.round(2.4)   //返回2

Math.round(-1.5)  //返回-1

Math.round(-5.8)  //返回-6

二、parseInt

作用:解析一个字符串,并返回一个整数,这里可以简单理解成返回舍去参数的小数部分后的整数。

如:

parseInt(5.57)  //返回5

parseInt(2.4)  //返回2

parseInt(-1.5)  //返回-1

parseInt(-5.8)  //返回-5

三、Math.floor(地板,向下取)

作用:返回小于等于参数的最大整数。

如:

Math.floor(5.57)  //返回5

Math.floor(2.4)  //返回2

Math.floor(-1.5)  //返回-2

Math.floor(-5.8)  //返回-6

四、Math.ceil(天花板,向上取)

作用:返回大于等于参数的最小整数

Math.ceil(5.57)  //返回6

Math.ceil(2.4)  //返回3

Math.ceil(-1.5)  //返回-1

Math.ceil(-5.8)  //返回-5

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

标签:简单   返回   解析   最小   字符串   logs   等于   tle   title   

原文地址:https://www.cnblogs.com/lianghong/p/8424584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!