Java Math ceil() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.ceil(a) returns the smallest whole number that is greater than or equal to a. In plain words: it rounds up. Math.ceil(4.1) is 5.0, and Math.ceil(4.0) stays 4.0.

"Up" means toward positive infinity, so negative numbers move toward zero: Math.ceil(-2.7) is -2.0. Use it for "how many containers do I need" questions: pages, boxes, buses, billed minutes.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.ceil(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a double. The result is a double holding a whole number, so cast it if you need an int.

Java
double a = Math.ceil(4.1);                         // 5.0
double b = Math.ceil(-2.7);                        // -2.0
int pages = (int) Math.ceil(23 / 10.0);            // 3
int wrong = (int) Math.ceil(23 / 10);              // 2 (int division first!)
CallWhat it does
Math.ceil(7.01)Returns 8.0.
Math.ceil(7.0)Already whole: returns 7.0.
(int) Math.ceil((double) items / perPage)Number of pages or boxes needed.
Math.ceil(-0.5)Returns -0.0 (a negative zero).

For whole-number division you can avoid doubles completely: (items + perPage - 1) / perPage works for positive ints, and Java 18 adds Math.ceilDiv.

Library declarations
Java
// java.lang.Math
public static double ceil(double a)

Parameters

ParameterDescription
aThe value to round up. int, long, and float arguments are widened to double.

Return Value

Return ValueDescription
doubleThe smallest whole-number double that is >= a. NaN, infinities, zeros, and values that are already whole are returned unchanged.

Compare with Similar Methods

The rounding family at a glance:

MethodHow it differs
floor(a)Rounds down (toward negative infinity).
round(a)Rounds to the nearest whole number and returns a long.
rint(a)Nearest whole number as a double; ties go to even.
ceilDiv(x, y)Integer division rounded up, no doubles involved. Java 18+.

Java Examples

Example 1 — Count Pages, and the Integer-Division Trap

23 items fit 10 to a page. How many pages do you need?

Java
public class MathCeilPages {
    public static void main(String[] args) {
        int items = 23;
        int perPage = 10;
        int pages = (int) Math.ceil((double) items / perPage);
        System.out.println("Pages needed: " + pages);
        System.out.println("Wrong (int division first): " + Math.ceil(items / perPage));
    }
}

How It Works

  1. Divide as doubles. The cast makes the division 23.0 / 10, which is 2.3; ceil rounds that up to 3.0, and (int) turns it into 3.
  2. The trap. items / perPage with two ints is integer division, which already threw away the .3 and produced 2.
  3. Nothing to round. Math.ceil(2) receives a whole number, so it returns 2.0, and the last three items would get no page.

Example 2 — Bill Started Minutes and Book Enough Buses

A taxi charges 0.5 per started minute, and a school trip needs enough 48-seat buses.

Java
public class MathCeilFare {
    public static void main(String[] args) {
        double minutes = 12.2;
        double started = Math.ceil(minutes);
        System.out.println("Billed minutes: " + started);
        System.out.println("Fare: " + started * 0.5);

        int students = 130;
        int seatsPerBus = 48;
        System.out.println("Buses: " + (int) Math.ceil(students / (double) seatsPerBus));
    }
}

How It Works

  1. Any part counts. 12.2 minutes means the 13th minute has started, so ceil bills 13.0 minutes.
  2. Exact money here. 13.0 * 0.5 is exactly 6.5 in binary, so the fare prints without rounding noise.
  3. Never leave anyone behind. 130 / 48.0 is about 2.71 buses; rounding up gives 3, because two buses hold only 96 students.

Example 3 — Negative Numbers, -0.0, and Huge Values

See what "up" means below zero, and which values pass through untouched.

Java
public class MathCeilNegative {
    public static void main(String[] args) {
        System.out.println("ceil(-2.7)  = " + Math.ceil(-2.7));
        System.out.println("ceil(-0.5)  = " + Math.ceil(-0.5));
        System.out.println("ceil(4.0)   = " + Math.ceil(4.0));
        System.out.println("ceil(1e300) = " + Math.ceil(1e300));
        System.out.println("ceil(NaN)   = " + Math.ceil(Double.NaN));
    }
}

How It Works

  1. Up means toward positive infinity. -2.0 is greater than -2.7, so rounding up moves a negative number closer to zero.
  2. A negative zero. -0.5 rounds up to zero, but the result keeps the minus sign of the input, so it prints as -0.0.
  3. Unchanged values. 4.0 is already whole, every double as large as 1e300 is whole, and NaN stays NaN; Java prints the big value in E notation as 1.0E300.

Frequently Asked Questions

It rounds a number up to the nearest whole number and returns it as a double. Math.ceil(4.1) returns 5.0.
Because many doubles are far too large for an int or long. Cast the result yourself, for example (int) Math.ceil(x), when you know it fits.
Because 23 / 10 is integer division, which already gives 2 before ceil runs. Write Math.ceil(23 / 10.0) or cast one operand to double.
It moves toward zero, because zero is the bigger direction. Math.ceil(-2.7) is -2.0, and Math.ceil(-0.5) is -0.0.
ceil always rounds up, so 4.1 becomes 5.0. round goes to the nearest whole number, so 4.1 becomes 4, and it returns a long instead of a double.

Next: ceilDiv()

Integer division that rounds the quotient up, toward positive infinity (Java 18+).

ceilDiv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful