JavaScript Math.ceil() | Round Up to nearest integer

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

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

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

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

JavaScript Math.ceil() Syntax

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

Math.ceil(x)

The parameter x is required, refers to a number, which is going to round Up to its nearest integer value.

JavaScript Math.ceil() Example

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

   <p>Math.ceil(-5.01) = <span id="span1"></span></p>
   <p>Math.ceil(-5.4) = <span id="span2"></span></p>
   <p>Math.ceil(-5.5) = <span id="span3"></span></p>
   <p>Math.ceil(-5.9) = <span id="span4"></span></p>
   <p>Math.ceil(0) = <span id="span5"></span></p>
   <p>Math.ceil(5.01) = <span id="span6"></span></p>
   <p>Math.ceil(5.4) = <span id="span7"></span></p>
   <p>Math.ceil(5.5) = <span id="span8"></span></p>
   <p>Math.ceil(5.9) = <span id="span9"></span></p>

  <script>
    document.getElementById("span1").innerHTML = Math.ceil(-5.01);
    document.getElementById("span2").innerHTML = Math.ceil(-5.4);
    document.getElementById("span3").innerHTML = Math.ceil(-5.5);
    document.getElementById("span4").innerHTML = Math.ceil(-5.9);
    document.getElementById("span5").innerHTML = Math.ceil(0);
    document.getElementById("span6").innerHTML = Math.ceil(5.01);
    document.getElementById("span7").innerHTML = Math.ceil(5.4);
    document.getElementById("span8").innerHTML = Math.ceil(5.5);
    document.getElementById("span9").innerHTML = Math.ceil(5.9);
  </script>
   
</body>
</html>
Output

Math.ceil(-5.01) =

Math.ceil(-5.4) =

Math.ceil(-5.5) =

Math.ceil(-5.9) =

Math.ceil(0) =

Math.ceil(5.01) =

Math.ceil(5.4) =

Math.ceil(5.5) =

Math.ceil(5.9) =

Please note: -5 is greater than -5.01, -5.4, 5.5, and -5.9.

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.floor() - round a number up to its nearest smallest integer.
  • Math.round() - round a specified number to its nearest integer value.
  • 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 😋