rounding
Math.ceil
Returns the smallest integer greater than or equal to a given number (rounds toward +Infinity). Always moves right on the number line.
Math.ceil(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | The number to ceil; non-numeric input is coerced |
| negative values | Ceils TOWARD zero: Math.ceil(-2.9) === -2 (not -3) |
| -0 handling | Math.ceil(-0.5) returns -0, preserving the negative sign |
Examples
console.log(Math.ceil(2.1)); // 3 Any positive fraction rounds up to the next integer.
console.log(Math.ceil(-2.9)); // -2 For negatives, ceil moves toward zero, opposite of floor.
console.log(Math.ceil(5)); // 5 Integer inputs return unchanged.
console.log(Math.ceil(1/0)); // Infinity Infinity is returned as-is.
Gotcha
For pagination: Math.ceil(total / perPage) is the correct pattern; using Math.round leaves the last partial page unreachable.
Related
Math.floor
Returns the largest integer less than or equal to a given number (rounds toward -Infinity). Always moves left on the number line.
Math.round
Returns the value of a number rounded to the nearest integer. Halfway values (.5) always round toward +Infinity, not away from zero.
Math.trunc
Returns the integer part of a number by removing any fractional digits — rounding toward zero. Introduced in ES2015.
Math.sign
Returns the sign of a number as 1, -1, 0, -0, or NaN. Introduced in ES2015 as a compact replacement for the ternary `x > 0 ? 1 : x < 0 ? -1 : 0`.