码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript Math ceil()、floor()、round()三个函数的区别

时间:2015-07-03 20:41:26      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

Round是四舍五入的。。。Ceiling是向上取整。。float是向下取整 

下面来介绍将小数值舍入为整数的几个方法:Math.ceil()、Math.floor()和Math.round()。 这三个方法分别遵循下列舍入规则:
◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;
◎Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。

下面是使用这些方法的示例:

对于所有介于25和26(不包括26)之间的数值,Math.ceil()始终返回26,因为它执行的是向上舍入。Math.round()方法只在数值大于等于25.5时返回26;否则返回25。最后,Math.floor()对所有介于25和26(不包括26)之间的数值都返回25。

以下是一些补充:
ceil():将小数部分一律向整数部分进位。
如:

 $(this).animate({left:"+=500px",height:"300px",opacity:"0.5"},3000)

 


Math.ceil(12.2)//返回13
Math.ceil(12.7)//返回13
Math.ceil(12.0)// 返回12

floor():一律舍去,仅保留整数。
如:

Math.floor(12.2)// 返回12
Math.floor(12.7)//返回12
Math.floor(12.0)//返回12

round():进行四舍五入
如:

Math.round(12.2)// 返回12
Math.round(12.7)//返回13
Math.round(12.0)//返回12

 $(this).animate({left:"+=500px",height:"300px",opacity:"0.5"},3000)

Javascript Math ceil()、floor()、round()三个函数的区别

标签:

原文地址:http://www.cnblogs.com/huo2s/p/4619562.html

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