JavaScript Math.round() | Round a number to its nearest integer

The JavaScript Math.round() method is used when we need to round a specified number to its nearest integer value. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

  <script>
    let num = 10.51;
    document.getElementById("xyz").innerHTML = Math.round(num);
  </script>
   
</body>
</html>
Output

Please note: While rounding a number using the Math.round() method:

JavaScript Math.round() Syntax

The syntax of Math.round() method in JavaScript is:

Math.round(x)

The parameter x is required, refers to a number.

The Math.round(x) method returns a number indicates to the rounded value of x.

JavaScript Math.round() Example

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

  <p>Math.round(-5.1) = <span id="span1"></span></p>
  <p>Math.round(-5.49) = <span id="span2"></span></p>
  <p>Math.round(-5.50) = <span id="span3"></span></p>
  <p>Math.round(-5.51) = <span id="span4"></span></p>
  <p>Math.round(0) = <span id="span5"></span></p>
  <p>Math.round(5.1) = <span id="span6"></span></p>
  <p>Math.round(5.49) = <span id="span7"></span></p>
  <p>Math.round(5.50) = <span id="span8"></span></p>
  <p>Math.round(5.51) = <span id="span9"></span></p>

  <script>
    document.getElementById("span1").innerHTML = Math.round(-5.1);
    document.getElementById("span2").innerHTML = Math.round(-5.49);
    document.getElementById("span3").innerHTML = Math.round(-5.50);
    document.getElementById("span4").innerHTML = Math.round(-5.51);
    document.getElementById("span5").innerHTML = Math.round(0);
    document.getElementById("span6").innerHTML = Math.round(5.1);
    document.getElementById("span7").innerHTML = Math.round(5.49);
    document.getElementById("span8").innerHTML = Math.round(5.50);
    document.getElementById("span9").innerHTML = Math.round(5.51);
  </script>
   
</body>
</html>
Output

Math.round(-5.1) =

Math.round(-5.49) =

Math.round(-5.50) =

Math.round(-5.51) =

Math.round(0) =

Math.round(5.1) =

Math.round(5.49) =

Math.round(5.50) =

Math.round(5.51) =

Other methods to do similar job

Here are the list of methods that are used to either round a number in other way, or to do similar type of job.

JavaScript Online Test


« Previous Tutorial Next Tutorial »