The JavaScript Math.ceil() method
The Math.ceil() method rounds a number up to the nearest integer. Math.ceil(4.1) is 5, and Math.ceil(-4.9) is -4 — it always goes toward positive infinity. It's the opposite of Math.floor(), and the usual choice for "how many pages/groups do I need".
Overview
Math.ceil() always rounds up to the nearest whole number ("ceiling"). 4.1 becomes 5, and so does 4.9 — any fractional part rounds up. It's the mirror image of Math.floor(), which always rounds down.
Its classic use is the "how many do I need" calculation. To fit 23 items into pages of 10, you need Math.ceil(23 / 10) = 3 pages — because the leftover 3 still needs a whole page. The same logic covers groups, rows, batches and progress thresholds, anywhere a partial unit still requires a full one.
For negatives, "up" means toward positive infinity, so Math.ceil(-4.9) is -4, not -5. It rounds out the rounding family: ceil up, floor down, round to nearest, and trunc toward zero.
Syntax
Math.ceil(4.1) // 5
Math.ceil(4.0) // 4
Math.ceil(-4.9) // -4 (toward positive infinity)
Math.ceil(total / perPage) // page count
Parameters
The Math.ceil() method accepts the following parameters.
| Parameter | Description |
|---|---|
x |
The number to round up to the nearest integer. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const items = 23, perPage = 10;
const pages = Math.ceil(items / perPage);
document.getElementById('out').textContent =
items + ' items need ' + pages + ' pages'; // 23 items need 3 pages
</script>
Best practices
Frequently asked questions
What does Math.ceil() do?
Math.ceil(4.1) is 5.How do I calculate the number of pages?
Math.ceil(totalItems / itemsPerPage), since any remainder needs an extra page.What is the difference between Math.ceil() and Math.floor()?
Math.ceil() always rounds up; Math.floor() always rounds down.What does Math.ceil(-4.5) return?
-4 — "up" means toward positive infinity, so it rounds toward zero for negatives.