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

四舍五入round()

时间:2017-05-24 19:19:19      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:wan   个数   math   技术   ext   pre   dede   class   code   

四舍五入round()

round() 方法可把一个数字四舍五入为最接近的整数。

语法:

Math.round(x)

参数说明:

技术分享

注意:

1. 返回与 x 最接近的整数。

2. 对于 0.5,该方法将进行上舍入。(5.5 将舍入为 6)

3. 如果 x 与两侧整数同等接近,则结果接近 +∞方向的数字值 。(如 -5.5 将舍入为 -5; -5.52 将舍入为 -6),如下图:

技术分享

把不同的数舍入为最接近的整数,代码如下:

<script type="text/javascript">
  document.write(Math.round(1.6)+ "<br>");
  document.write(Math.round(2.5)+ "<br>");
  document.write(Math.round(0.49)+ "<br>");
  document.write(Math.round(-6.4)+ "<br>");
  document.write(Math.round(-6.6));
</script>

运行结果:

2
3
0
-6
-7

四舍五入round()

标签:wan   个数   math   技术   ext   pre   dede   class   code   

原文地址:http://www.cnblogs.com/codepen2010/p/6900471.html

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