JavaScript 中的 Math.round() 函数


Math 对象的 round() 函数接受一个浮点数随机数并返回其最接近的整数。

  • 如果给定的数字是 x.5 或更大,此函数将返回下一个数字 (x + 1)
  • 如果给定数字是 x.4 或更小,此函数将返回上一个数字 (x - 1)
  • 如果给定数字本身是一个整数,此函数将返回相同的值。

语法

其语法如下

Math.round();

示例

 在线演示

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var result = Math.round(2541.542);
      document.write("Rounded value of the given number: "+result);
   </script>
</body>
</html>

输出

Rounded value of the given number: 2542

更新于: 25-06-2020

128 次查看

启动你的 职业生涯

通过完成课程获得认证

马上开始
广告
© . All rights reserved.