Categories
JavaScript Web development

JavaScript Math.round()

JavaScript Math.round()

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

Syntax:

Math.round(number)

Note: 2.49 will be rounded to 2, and 2.5 will be rounded to 3.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.round(2.77);
let b = Math.round(2.43);
let c = Math.round(2.50);
let d = Math.round(-2.65);
let e = Math.round(-2.23);
let f = Math.round(-2.49);

document.getElementById("example").innerHTML = 
a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e + "<br>" + f;
</script>

</body>
</html>

Output:

Math.round() rounds a number to the nearest integer:

Enjoy coding!

Read also:

JavaScript Math.fround()

JavaScript random() Method

JavaScript Math Object