Round a Number to the Next Integer using JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 27, 2021

banner

Check out my new book

Round a Number to the Next Integer #

Use the Math.ceil() function to round a number to the next integer. The Math.ceil function takes a number as a parameter, rounds the number up to the next largest integer and returns the result.

index.js
// ✅️ Math.ceil = Always round up to NEXT integer // ✅ Math.round = round to NEAREST integer console.log(Math.ceil(3.01)); // 👉️ 4 console.log(Math.round(3.01)); // 👉️ 3 console.log(Math.ceil(3.5)); // 👉️ 4 console.log(Math.round(3.5)); // 👉️ 4

We used the Math.ceil function to round a number to the next integer.

The function returns the smallest integer that is greater than or equal to the provided number.

index.js
console.log(Math.ceil(0.1)); // 👉️ 1 console.log(Math.ceil(-1.05)); // 👉️ -1 console.log(Math.ceil(5.0001)); // 👉️ 6 console.log(Math.ceil(-5)); // 👉️ -5

Note that negative numbers also get rounded to the next greater or equal integer.

In short, if there is anything after the decimal, the number will get rounded to the next integer, otherwise the number is returned.

This is very different from the Math.round function, which rounds to the nearest integer.

index.js
console.log(Math.round(0.49)); // 👉️ 0 console.log(Math.round(0.5)); // 👉️ 1

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

Further Reading #

Use the search field on my Home Page to filter through my more than 3,000 articles.