Suppose you use the argument 4.5, and you want a number that is "greater than or equal to the argument and is equal to a mathematical integer".

There are lots of numbers that match those criteria: 5, 6, 7, 100, 2000, ....

The smallest of those (closest to negative infinity) is 5; and that's what ceil returns. The description is perfectly reasonable.

Answer from khelwood on Stack Overflow
🌐
Programiz
programiz.com › java-programming › library › math › ceil
Java Math ceil()
System.out.println(Math.ceil(a)); // 2.0 // value equals to 5 after decimal double b = 1.5;
🌐
W3Schools
w3schools.com › java › ref_math_ceil.asp
Java Math ceil() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... System.out.println(Math.ceil(0.60)); System.out.println(Math.ceil(0.40)); System.out.println(Math.ceil(5)); System.out.println(Math.ceil(5.1)); System.out.println(Math.ceil(-5.1)); System.out.println(Math.ceil(-5.9));
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_ceil.htm
Java - Math ceil(double) Method
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a double number double x = 10.7; // print the ceil of the number System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x)); } }
🌐
Codecademy
codecademy.com › docs › java › math methods › .ceil()
Java | Math Methods | .ceil() | Codecademy
December 12, 2022 - Returns the double value that is a mathematical integer and is greater than or equal to the original value.
Top answer
1 of 15
246

You are doing 157/32 which is dividing two integers with each other, which always result in a rounded down integer. Therefore the (int) Math.ceil(...) isn't doing anything. There are three possible solutions to achieve what you want. I recommend using either option 1 or option 2. Please do NOT use option 0.

Option 0

Convert a and b to a double, and you can use the division and Math.ceil as you wanted it to work. However I strongly discourage the use of this approach, because double division can be imprecise. To read more about imprecision of doubles see this question.

int n = (int) Math.ceil((double) a / b));

Option 1

int n = a / b + ((a % b == 0) ? 0 : 1); 

You do a / b with always floor if a and b are both integers. Then you have an inline if-statement which checks whether or not you should ceil instead of floor. So +1 or +0, if there is a remainder with the division you need +1. a % b == 0 checks for the remainder.

Option 2

This option is very short, but maybe for some less intuitive. I think this less intuitive approach would be faster than the double division and comparison approach:
Please note that this doesn't work for b < 0.

int n = (a + b - 1) / b;

To reduce the chance of overflow you could use the following. However please note that it doesn't work for a = 0 and b < 1.

int n = (a - 1) / b + 1;

Explanation behind the "less intuitive approach"

Since dividing two integers in Java (and most other programming languages) will always floor the result. So:

int a, b;
int result = a/b (is the same as floor(a/b) )

But we don't want floor(a/b), but ceil(a/b), and using the definitions and plots from Wikipedia:

With these plots of the floor and ceil functions, you can see the relationship.

You can see that floor(x) <= ceil(x). We need floor(x + s) = ceil(x). So we need to find s. If we take 1/2 <= s < 1 it will be just right (try some numbers and you will see it does, I find it hard myself to prove this). And 1/2 <= (b-1) / b < 1, so

ceil(a/b) = floor(a/b + s)
          = floor(a/b + (b-1)/b)
          = floor( (a+b-1)/b) )

This is not a real proof, but I hope you're satisfied with it. If someone can explain it better I would appreciate it too. Maybe ask it on MathOverflow.

2 of 15
62

157/32 is int/int, which results in an int.

Try using the double literal - 157/32d, which is int/double, which results in a double.

🌐
Javatpoint
javatpoint.com › java-math-ceil-method
Java Math.ceil() method with Examples - Javatpoint
The java.lang.Math.ceil () is used to find the smallest integer value that is greater than or equal to the argument or mathematical integer · Java method The java.lang. return the unbiased exponent used in the representation of double or float. Syntax public static int getExponent(float x) ...
🌐
Scaler
scaler.com › home › topics › math.ceil() in java
Math.ceil() in Java - Scaler Topics
May 5, 2024 - The ceil function returns a double value, which is equal to the nearest Mathematical integer greater than or equal to the passed parameter.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-ceil-method-examples
Java ceil() method with Examples - GeeksforGeeks
January 21, 2026 - The Math.ceil() method in Java is used to return the smallest integer value that is greater than or equal to a given number. The returned value is of type double and represents the mathematical ceiling of the argument.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › ceil
Java Math ceil() - Round Up Value | Vultr Docs
September 27, 2024 - Use Math.ceil() to round the number to the nearest integer. ... This code rounds the value 3.14 up to 4.0.
🌐
Medium
medium.com › @AlexanderObregon › rounding-numbers-with-math-round-math-floor-and-math-ceil-in-java-d201bbeb85e2
Java’s Math Rounding Methods Explained | Medium
March 7, 2025 - In a ride-sharing app, for example, if a trip covers 3.2 miles, it’s fairer to charge for 4 miles rather than 3, since the driver still had to complete most of the fourth mile. Memory allocation is another case where rounding up is necessary. If a program needs 4.1 MB to run properly, rounding down to 4 MB wouldn’t be enough. Using Math.ceil() makes sure it gets 5 MB, avoiding unexpected failures due to insufficient memory.
🌐
How to do in Java
howtodoinjava.com › home › java math.ceil() vs. math.floor() vs. math.round()
Java Math.ceil() vs. Math.floor() vs. Math.round()
September 6, 2023 - The rounded value 5.0 is mathematically equal to integer 5. double number = 4.3; double roundedUp = Math.ceil(number); // 5.0
🌐
CodeGym
codegym.cc › java blog › java math › java math.ceil() method
Java Math.ceil() method
October 11, 2023 - If the parameter is “null” - unlike the mathematical ceil function where you get a zero, here you’ll get a ... class Main { public static void main(String[] args) { Double totalStudentsInClass = 25.0; Double flourWeightInKgs = 5.13; Double aPoundOfOxygenInLitres = 0.3977; Double startingArrayIndexInJava = 0.0; Double aSelfDrivingCar = Double.NaN; Double numberOfStarsInTheSky = Double.POSITIVE_INFINITY; // For parameter [ -1 > x < 0 ] Double x = -0.025; // using Math.ceil() method System.out.println("Total Students In Class = " + Math.ceil(totalStudentsInClass)); System.out.println("Flour
🌐
Tutorial Gateway
tutorialgateway.org › java-ceil-function
Java ceil Function
March 25, 2025 - In this math ceil example, we declared an Array of double types and assigned some random values. double [] CeilArray = {10.46, -15.98, 22.44, 95.9999, -4.8799, 12.8597}; Next, We used Java For Loop to iterate the Array.
🌐
CodeAhoy
codeahoy.com › java › Math-Ceil-method-JI_15
Java Math.ceil() Method with Examples | CodeAhoy
October 12, 2019 - public class MathEx { public static void main(String[] args) { ceiling(); } private static void ceiling() { System.out.println(Math.ceil(5.1)); // 6.0 System.out.println(Math.ceil(3.8)); // 4.0 System.out.println(Math.ceil(7.0)); // 7.0 System.out.println(Math.ceil(-7.0)); // -7.0 // cast to int System.out.println((int) Math.ceil(17.412)); // 18 System.out.println(Math.ceil(Double.NaN)); // NaN } } Output ·
🌐
Quora
quora.com › What-is-the-difference-between-Math-round-and-Math-ceil-in-Java-Which-one-is-recommended-for-rounding-off-numbers-and-why
What is the difference between Math.round and Math.ceil in Java? Which one is recommended for rounding off numbers and why? - Quora
Answer: Math.round(m) returns the integer closest to m. In case of a tie (decimal part being 0.5), it rounds towards positive infinity. So 4.5 rounds to 5, -4.5 rounds to -4. Math.floor(m) returns the largest integer less than or equal to m, whereas Math.ceil(m) returns the smallest integer larg...
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-ceil-method
Java Math.ceil() Method
October 9, 2022 - For example, Math.ceil(9.9) would return 10. In this guide, we will discuss the ceil() method with examples. public class JavaExample { public static void main(String[] args) { double n1 = 5.55, n2 = -5.55; System.out.println(Math.ceil(n1)); System.out.println(Math.ceil(n2)); } } Output: 6.0 -5.0 ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
1 week ago - If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first argument to the power of the second argument if that result can in fact be represented exactly as a double value. (In the foregoing descriptions, a floating-point value is considered to be an integer if and only if it is finite and a fixed point of the method ceil or, equivalently, a fixed point of the method floor.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Math ceil(), floor(), round() Methods - Java Code Geeks
January 3, 2024 - When you run this program, you’ll get output similar to the following: ... In conclusion, when working with numerical values in Java, the Math class provides three essential methods for rounding: Math.ceil() for rounding up to the nearest integer, ...