Math.round()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Math.round() 関数は、引数として与えた数を四捨五入して、もっとも近似の整数を返します。

試してみましょう

console.log(Math.round(0.9));
// Expected output: 1

console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
// Expected output: 6 6 5

console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
// Expected output: -5 -5 -6

構文

js
Math.round(x)

引数

x

数値です。

返値

x の値をもっとも近似の整数に四捨五入した値。

解説

引数の小数部分が 0.5 以上の場合、その引数は、次に大きい整数に切り上げられます。引数の小数部分が 0.5 未満の場合、その引数は、次に小さい整数に切り下げられます。小数部分が 0.5 である場合は、正の無限大の方向で次の整数に丸められます。これは多くの言語の round() 関数と異なることに注意してください。この場合はたいてい、0 から遠ざかる次の整数に丸められます (小数部分が 0.5 である負の値を四捨五入する場合に、結果が変わります)。

メモ: これは、多くの言語の round() 関数とは異なります。これらの関数は、半増分をゼロから離れた方向へ丸めることが多く、小数部分がちょうど 0.5 の負数の場合は異なる結果となります。

Math.round(x) は、 Math.floor(x + 0.5) とまったく同じではありません。 x が -0、または -0.5 ≤ x < 0 の場合、 Math.round(x) は -0 を返し、一方、 Math.floor(x + 0.5) は 0 を返します。しかし、この違いや潜在的な精度エラーを無視すれば、 Math.round(x)Math.floor(x + 0.5) は一般的に同等です。

round()Math オブジェクトの静的なメソッドなので、自ら生成した Math オブジェクトのメソッドとしてではなく、常に、Math.round() として使用するようにしてください (Math のコンストラクターはありません)。

round の使用

js
Math.round(-Infinity); // -Infinity
Math.round(-20.51); // -21
Math.round(-20.5); // -20
Math.round(-0.1); // -0
Math.round(0); // 0
Math.round(20.49); // 20
Math.round(20.5); // 21
Math.round(42); // 42
Math.round(Infinity); // Infinity

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-math.round

ブラウザーの互換性

BCD tables only load in the browser

関連情報