JS对象 四舍五入round() round() 方法可把一个数字四舍五入为最接近的整数。 语法: Math.round(x)...

四舍五入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

任务

补充右边编辑器代码,在script标签内,使用round()方法,计算3.3、-0.1、-9.9、8.9值。


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Math </title>
<script type="text/javascript">
    document.write(Math.round(3.3)+"<br/>");
    document.write(Math.round(-0.1)+"<br/>");
    document.write(Math.round(-9.9)+"<br/>");
    document.write(Math.round(8.9));
</script>
</head>
<body>
</body>
</html>



转载于:https://www.cnblogs.com/yongbin668/p/5592750.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值