码迷,mamicode.com
首页 > 其他好文 > 详细

Math.round(),Math.ceil(),Math.floor()的区别

时间:2019-08-31 17:23:19      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:ceil   数加   理解   根据   总结   math   等于   正数   div   

1.Math.round():根据“round”的字面意思“附近、周围”,可以猜测该函数是求一个附近的整数,看下面几个例子就明白。

小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11
 
小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12
 
小数点后第一位=5
正数:Math.round(11.5)=12
负数:Math.round(-11.5)=-11
总结:(小数点后第一位)大于五全部加,等于五正数加,小于五全不加。
 
2.Math.ceil():根据“ceil”的字面意思“天花板”去理解;
例如:
Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12
Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-11
 
3.Math.floor():根据“floor”的字面意思“地板”去理解;
例如:
Math.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11
Math.floor(-11.46)=Math.floor(-11.68)=Math.floor(-11.5)=-12

Math.round(),Math.ceil(),Math.floor()的区别

标签:ceil   数加   理解   根据   总结   math   等于   正数   div   

原文地址:https://www.cnblogs.com/andyZhang0511/p/11439419.html

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