JavaScript round() method
- Previous Page random()
- Next Page sign()
- Go to the Previous Level JavaScript Math Reference Manual
Definition and usage
round()
The method rounds a number to the nearest integer.
Comment:2.49 will be rounded down (2), while 2.5 will be rounded up (3).
Note
For 0.5, the method will perform rounding up.
For example, 3.5 will be rounded up to 4, while -3.5 will be rounded down to -3.
Example
Example 1
Round a number to the nearest integer:
Math.round(2.5);
Example 3
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);
Syntax
Math.round(x)
Parameter value
Parameter | Description |
---|---|
x | Required. The number to be rounded. |
Technical details
Return value: | A number, representing the nearest integer. |
---|---|
JavaScript Version: | ECMAScript 1 |
Browser Support
Method | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
round() | Support | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Mathematics
- Previous Page random()
- Next Page sign()
- Go to the Previous Level JavaScript Math Reference Manual