javascript,小数值舍入操作方法:ceil()、floor()、round()

Math对象中有3个方法用于处理小数值的舍入操作,它们是:Math.ceil()、Math.floor()、Math.round()。

Math.ceil():向上舍入为最接近的整数。

Math.floor():向下舍入为最接近的整数。

Math.round():标准的四舍五入。

alert(Math.ceil(3.2));   // 4
alert(Math.ceil(3.5));   // 4
alert(Math.ceil(3.6));   // 4

alert(Math.floor(3.2));  // 3
alert(Math.floor(3.5));  // 3
alert(Math.floor(3.6));  // 3

alert(Math.round(3.2));  // 3
alert(Math.round(3.5));  // 4
alert(Math.round(3.6));  // 4

 

 

posted @ 2013-03-20 23:08  白色的海  阅读(1398)  评论(0编辑  收藏  举报