Round required capacity up
Normalize measurements or prices upward to whole units when any fractional remainder requires another unit.
JavaScript · Function
Math.ceil() returns the smallest integer greater than or equal to a number. It rounds toward positive infinity, so negative fractional values move toward zero.
Math.ceil(x) returns the smallest integer greater than or equal to x. For negative values, “up” means toward positive infinity, so -7.004 becomes -7; the result can preserve negative zero.
Math.ceil(x)| Name | Type | Description |
|---|---|---|
x | number | The numeric value to round upward. |
The smallest integer greater than or equal to x.
const values = [0.95, 4, 7.004, -7.004, -0.95];
const roundedUp = values.map(Math.ceil);
console.log(roundedUp); // [1, 4, 8, -7, -0]Maps each number to the smallest integer that is greater than or equal to it.
Normalize measurements or prices upward to whole units when any fractional remainder requires another unit.
Convert a count of items into the number of fixed-size pages or batches needed.
Choose the next integer threshold for indexes, limits, or discrete scheduling slots.
Math.ceil(x) returns the smallest integer greater than or equal to x. Thus Math.ceil(7.004) is 8, while Math.ceil(-7.004) is -7.
Follow-up: How can the operation be expressed using Math.floor()? As -Math.floor(-x), which gives the same result.
It is a static method on the Math object, so the call form is Math.ceil(x). Math is not a constructor and does not need to be instantiated.
Follow-up: What happens at the infinities? Math.ceil(Infinity) remains Infinity and Math.ceil(-Infinity) remains -Infinity.
The parameter x is a number, and the result is the smallest integer greater than or equal to x. Integral inputs are unchanged, so Math.ceil(4) returns 4.
Follow-up: What special signed-zero case should be tested? Math.ceil(-0.95) returns -0, and Math.ceil(-0) also returns -0.
Math.floor()Math.round()Math.trunc()Math