Math对象用于执行数学任务。
floor()方法可对一个数向下取舍。
语法
Math.floor(x)
floor()方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。
例如:
<script type="text/javascript"> document.write(Math.floor(0.60) + "<br />") document.write(Math.floor(0.40) + "<br />") document.write(Math.floor(5) + "<br />") document.write(Math.floor(5.1) + "<br />") document.write(Math.floor(-5.1) + "<br />") document.write(Math.floor(-5.9)) </script>
输出:
0 0 5 5 -6 -6
round()方法可把一个数字舍入为最接近的整数。
Math.round(x)
round()方法执行的是四舍五入
例如:
<script type="text/javascript"> document.write(Math.round(0.60) + "<br />") document.write(Math.round(0.50) + "<br />") document.write(Math.round(0.49) + "<br />") document.write(Math.round(-4.40) + "<br />") document.write(Math.round(-4.60)) </script>
输出:
1 1 0 -4 -5
ceil()方法可对一个数进行上舍入。
Math.ceil(x)
ceil()方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。
例如:
<script type="text/javascript"> document.write(Math.ceil(0.60) + "<br />") document.write(Math.ceil(0.40) + "<br />") document.write(Math.ceil(5) + "<br />") document.write(Math.ceil(5.1) + "<br />") document.write(Math.ceil(-5.1) + "<br />") document.write(Math.ceil(-5.9)) </script>
输出:
1 1 5 6 -5 -5