JavaScript Math round() 方法

round() — 把一个数字舍入为最接近的整数

JavaScript Math round() 方法语法

  Math.round(x);

JavaScript Math round() 方法参数说明

x — 待处理的数字

JavaScript Math round() 方法返回值

与 x 最接近的整数

说明:

对于 0.5,该方法将进行上舍入。

( 例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3。)

JavaScript Math round() 方法示例

  console.log( Math.round(0.80) );
  console.log( Math.round(0.50) );
  console.log( Math.round(0.49) );
  console.log( Math.round(-4.40) );
  console.log( Math.round(-4.50) );
  console.log( Math.round(-4.60) );

JavaScript Math round() 方法结果

  >>>
  1
  1
  0
  -4
  -4
  -5

最后修改:2023 年 05 月 31 日
如果觉得我的文章对你有用,请随意赞赏