您的位置:首页 > 其它

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

2018-03-23 15:59 459 查看

转自:https://www.cnblogs.com/johnsonwei/p/6101171.html

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

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.1)=Math.ceil(11.5)=Math.ceil(11.9)=12Math.ceil(-11.1)=Math.ceil(-11.5)=Math.ceil(-11.9)=-11 因为 12>11 、-11>-12 所以 Math.ceil() 取值靠近大的一方3.Math.floor():根据“floor”的字面意思“地板”去理解;例如:Math.ceil(11.12)=Math.ceil(11.55)=Math.ceil(11.89)=11Math.ceil(-11.12)=Math.ceil(-11.55)=Math.ceil(-11.89)=-12因为 11<12 、-12<-11 所以 Math.ceil() 取值靠近小的一方
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: