Math.ceil()

Rounds a number up to the smallest integer greater than or equal to it.

Signature
Math.ceil(x)
  • Added in JavaScript ES1

Parameters

ParameterTypeDescription
$xnumberA number to round up. Non-numbers are coerced first.

Return value

numberThe smallest integer greater than or equal to x. Equivalent to -Math.floor(-x).

Math.ceil() rounds toward positive infinity: 7.004 becomes 8, and 4 stays 4 because the value is already an integer.

When to reach for it

Reach for it whenever a partial unit still costs a whole unit. Page counts (Math.ceil(total / perPage)), container counts, minimum batches, and "how many trucks do I need" problems are all ceiling problems. Using Math.round() there is a real bug: 21 items at 10 per page rounds to 2 pages and loses the last item.

What trips people up

For negative inputs, rounding up means rounding toward zero. Math.ceil(-7.004) is -7, not -8. If you wanted "away from zero" you want Math.sign(x) * Math.ceil(Math.abs(x)) instead.

The second surprise is negative zero. Any value in the range -1 < x < 0 ceils to -0, so Math.ceil(-0.95) is -0. It compares equal to 0 with ===, but it stringifies as "0" in template literals while Object.is and console.log can reveal the sign — and 1 / Math.ceil(-0.5) is -Infinity. Add 0 to normalise it if it will be displayed or divided into.

Rounding to a decimal place uses the multiply-and-divide trick, which inherits floating-point error; do not rely on it for currency.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Computing how many pages are needed for N results at a fixed page size
  • Sizing a fixed-capacity batch or container count from a total
  • Rounding a price or duration up to the next whole billing unit
  • Snapping a value up to the next multiple of 5, 10, or 100

Frequently asked questions

How do I always round up in JavaScript?

Use Math.ceil(x), which returns the smallest integer greater than or equal to x. Be aware that for negative numbers this rounds toward zero, so Math.ceil(-2.5) is -2.

Why does Math.ceil() return -0?

Any input strictly between -1 and 0 has -0 as its smallest greater-or-equal integer, so Math.ceil(-0.4) is -0. It equals 0 under === but keeps its sign, so add 0 to the result if the sign would be visible.

How do I round up to two decimal places?

Use Math.ceil(n * 100) / 100. This is fine for display but inherits floating-point error from the multiplication, so store currency as integer cents rather than rounding floats.

More examples

Rounding up to a multiple

Dividing by the step, ceiling, then multiplying back snaps a value up to the next multiple. An exact multiple is left alone. The decimal variant works for display, but n * 100 introduces representation error, so it is not safe for money — use integer cents or Intl.NumberFormat instead.

Example · javascript
function ceilTo(value, step) {
  return Math.ceil(value / step) * step;
}

console.log(ceilTo(123, 10));  // 130
console.log(ceilTo(130, 10));  // 130
console.log(ceilTo(4.2, 0.5)); // 4.5

// Decimal places, with the usual float caveat
const round2Up = (n) => Math.ceil(n * 100) / 100;
console.log(round2Up(1.234)); // 1.24
console.log(round2Up(1.005)); // 1.01

Ceil compared with the other rounding methods

The table shows the four methods diverging on negatives: ceil and trunc agree for negative inputs (both move toward zero) while floor and round do not. For -0.5, ceil gives -0 and round gives -0 too, since Math.round breaks ties toward +Infinity.

Example · javascript
const values = [2.5, 2.4, -2.4, -2.5, -0.5];

for (const v of values) {
  console.log(
    v,
    "ceil:", Math.ceil(v),
    "floor:", Math.floor(v),
    "round:", Math.round(v),
    "trunc:", Math.trunc(v)
  );
}

Discussion

  • Be the first to comment on this lesson.