Math.round
ExamplesΒΆThe general syntax for this method is:
Math.round(number)
This method returns number
rounded to the nearest integer value.
Numerical strings can also be evaluated, but should be avoided as a best practice.
Example
1console.log(Math.round(1.33));
2console.log(Math.round(-28.7));
3console.log(Math.round(8.5));
4console.log(Math.round("101.45"));
Console Output
1
-29
9
101
Math.round
also works on arrays, but must be combined with the
map array method. The syntax for this is:
arrayName.map(Math.round)
Example
1let numbers = [1.33, 4, 8.5, -15.523, 8.49];
2
3console.log(numbers.map(Math.round));
Console Output
[ 1, 4, 9, -16, 8 ]