A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Math/round below:

Math.round() - JavaScript | MDN

Math.round()

Baseline Widely available

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
構文 引数 返値

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 の使用
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
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4