JavaScriptNumbers and Math
Math. ceil()
Rounds a number up, to the nearest larger integer.
- Returns
- The smallest integer greater than or equal to
x.
Example
#JavaScript
const lessons = 23
const perPage = 10
console.log(Math.ceil(lessons / perPage))
console.log(Math.ceil(4.01))
console.log(Math.ceil(-2.7))Console
3
5
-2
Definition and usage
#The Math.ceil() function returns the smallest integer that isn’t less than the given number. Any fraction, even 0.01, causes it to round up.
A typical use case is figuring out how many "full batches" you need: the number of pages in pagination, the number of buses for a trip, the number of weeks in a course.
For negative numbers it rounds toward zero: Math.ceil(-2.7) gives -2.
Syntax
#Syntax
Math.ceil(x)Parameters
#x
number
The number to round up.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- The number of pages in pagination is always
Math.ceil(totalItems / perPage). - It’s worth rounding an article’s reading time up in minutes too, so you don’t show "0 min".
Related entries
#Spotted a mistake or missing an example? Write to us.