JavaScript round()Method

Math 对象参考手册JavaScript Math Object

Example

The round() method rounds a number to the nearest integer:

Math.round(2.5);

Output:

3

Try it Yourself »

Definition and Usage

The round() method rounds a number to the nearest integer.

Note:2.49 will round to 2, 2.5 will round to 3.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the round() method


Syntax

Math.round(x)

Parameter Values

Parameter Description
x Required. Must be a number.

Return Value

Type Description
Number The nearest integer.

Technical Details

JavaScript Version: 1.0


More Examples

Example

Round different numbers 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, andfOutput:

3
3
2
-3
-2
-2

Try it Yourself »


Math 对象参考手册JavaScript Math Object

Other Extensions