Latest web development tutorials

JavaScript round () method

Math Object Reference JavaScript Math Object

Examples

round () method to a number rounded to the nearest integer:

Math.round(2.5);

Output:

3

try it"

Definition and Usage

round () method to a number rounded to the nearest integer.

Note: 2.49 rounded 2, 2.5 rounded 3.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support round () method


grammar

Math.round( x )

Parameter Value

参数 描述
x 必需。必须是数字。

return value

类型 描述
Number 最接近的整数。

technical details

JavaScript version: 1.0


More examples

Examples

The different numbers rounded to the nearest integer:

var a=Math.round(2.60);
var b=Math.round(2.50);
var c=Math.round(2.49);
var d=Math.round(-2.60);
var e=Math.round(-2.50);
var f=Math.round(-2.49);

a, b, c, d, e, and f output:

3
3
2
-3
-2
-2

try it"


Math Object Reference JavaScript Math Object