Math.ceil()
Rounds up to the nearest integer that is greater than or equal.
Available since: Java 1.0
Syntax
static double ceil(double a)Parameters
| Parameter | Type | Description |
|---|---|---|
| a | double | A decimal number. |
Return value
doubleA whole value as a double.
Examples
How many pages are needed for 23 records with 10 per page:
void main() {
int items = 23;
int perPage = 10;
int pages = (int) Math.ceil((double) items / perPage);
IO.println(pages);
IO.println(Math.ceil(-1.5));
}Output:
3
-1.0Notes
Without the cast to double, 23 / 10 is integer division equal to 2, and ceil() cannot fix that. For integers there is a shortcut without decimals: Math.ceilDiv(items, perPage) (Java 18).
See also
- Math.floor() — Rounds down to the nearest integer that is less than or equal.
- Math.round() — Rounds a number to the nearest integer.
Related recipes
In the tutorial
- Type conversion — Automatic widening, explicit (int) casts, turning strings into numbers with Integer.parseInt and numbers into strings.