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>

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

    let num = 10.51;
    document.getElementById("xyz").innerHTML = Math.round(num);

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

  • 10.49 will round to 10
  • 10.50 will round to 11

JavaScript Math.round() Syntax

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


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>

  <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>

    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);

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.

  • Math.ceil() - round a number up to its nearest largest integer.
  • Math.floor() - round a number up to its nearest smallest integer.
  • toFixed() - round a number to particular number of decimals.
  • toPrecision() - format a number to specific length.
  • Math.trunc() - find the integer part of a number.

JavaScript Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋