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.