JS学习笔记一

数值舍入方法

1.ceil()方法

将数值向上舍入为最近整数

alert(Math.ceil(6.1)); //"7"

alert(Math.ceil(6.5)); //"7"
alert(Math.ceil(6.9)); //"7"
不管6后面的小数是几,该方法总是向上舍入,结果都是7
 
2.floor()方法,数值向下舍入为最接近的整数
alert(Math.floor(6.1)); //"6"
alert(Math.floor(6.5)); //"6"
alert(Math.floor(6.9)); //"6"
不管6后面的小数位数是几,该方法总是向下舍入,结果都是6
 
3.round()方法,将四舍五入为最接近的整数
alert(Math.round(6.1)); //"6"
alert(Math.round(6.5)); //"7"
alert(Math.round(6.9)); //"7"
alert(Math.round(-6.9)); //"-6"
该方法严格上是四舍六入  对0.5要进行判断对待
该方法总结就是:数值加0.5,向下取整
原文地址:https://www.cnblogs.com/yjw520/p/14666378.html