Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Math.round()

BaselineWidely available

TheMath.round() static method returns the value of a number rounded to the nearest integer.

Try it

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

Syntax

js
Math.round(x)

Parameters

x

A number.

Return value

The value ofx rounded to the nearest integer.

Description

If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value. If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞.

Note:This differs from many languages'round() functions, which often round half-incrementsaway from zero, giving a different result in the case of negative numbers with a fractional part of exactly 0.5.

Math.round(x) is not exactly the same asMath.floor(x + 0.5). Whenx is -0, or -0.5 ≤ x < 0,Math.round(x) returns -0, whileMath.floor(x + 0.5) returns 0. However, neglecting that difference and potential precision errors,Math.round(x) andMath.floor(x + 0.5) are generally equivalent.

Becauseround() is a static method ofMath, you always use it asMath.round(), rather than as a method of aMath object you created (Math has no constructor).

Examples

Using round

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

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp