Skip to content

Using The Math.ceil() Function In Javascript To Round a Number Up

Using The Math.ceil() Function In Javascript To Round a Number Up

Introduction

To round a number up in Javascript, you can use the Math.ceil() function.

This function accepts a number as an argument, rounds it up, and then returns that number.

Using Math.ceil()

You can write a function to take a number and return the smallest integer greater than or equal to the input:

function ceilNumber(num) {
  let ceil = Math.ceil(num);
  console.log(ceil);
}

This function applies the Math.ceil() and logs the result.

You can see this in action below:

ceilNumber(5);
// 5

ceilNumber(5.001);
// 6

ceilNumber(5.999);
// 6

Math.ceil() vs Math.floor()

The Math.floor() function, unlike the Math.ceil() method, round the number down.

console.log(Math.ceil(5.999));
// 6

console.log(Math.floor(5.999));
// 5

You can learn more about it on Math.floor() Function in Javascript, Uses, Limitations, Type Coercion.