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.

Answer from martijnn2008 on Stack Overflow
🌐
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));
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - If the argument value is less than zero but greater than -1.0, then the result is negative zero. Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
Discussions

Why floor, round and ceil return double?
What if the number being passed into the function is higher than the biggest int? More on reddit.com
🌐 r/java
7
6
September 12, 2013
Why Do Both Math.Floor(double) and Math.Ceiling(double) return a double and not an int?

Because a Double can contain significantly larger values than an integer, you can't cast a Double to an Int without risking overflow. There's no reason for such simple functions to add such risk.

More on reddit.com
🌐 r/csharp
27
28
February 29, 2016
🌐
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.
🌐
Programiz
programiz.com › java-programming › library › math › ceil
Java Math ceil()
The ceil() method takes a single parameter. ... Note: The returned value will be the smallest value that is greater than or equal to the specified argument. class Main { public static void main(String[] args) { // Math.ceil() method // value greater than 5 after decimal double a = 1.878;
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.

🌐
CodeGym
codegym.cc › java blog › java math › java math.ceil() method
Java Math.ceil() method
October 11, 2023 - “A ceil function converts a decimal number to the immediate largest integer.” If the number passed is already a whole number or an integer, then the same number is the ceiling value.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › math methods › .ceil()
Java | Math Methods | .ceil() | Codecademy
December 12, 2022 - The Math.ceil() method returns the double value that is a mathematical integer and is greater than or equal to the original value. ... Looking for an introduction to the theory behind programming?
🌐
Scaler
scaler.com › home › topics › math.ceil() in java
Math.ceil() in Java - Scaler Topics
May 5, 2024 - ... Math.ceil() converts the number to the nearest integer greater than or equal to the given number; if an integer is passed as an argument, the result of Math.ceil() will be the same integer, but since Math.ceil() returns a double value the ...
🌐
Reddit
reddit.com › r/java › why floor, round and ceil return double?
r/java on Reddit: Why floor, round and ceil return double?
September 12, 2013 -

Might be stupid question, but this has always puzzled me about Java.

Why functions Math.floor(), Math.round() and Math.ceil() return double value?

I thought main point of those is to convert floating point number into whole number. So why not returning long or int? That way we don't have to manually cast everytime.

🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java math ceil() method
Java Math ceil() Method
September 1, 2008 - The Java Math ceil(double a) returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
🌐
Dot Net Perls
dotnetperls.com › math-ceil-java
Java - Math.ceil Method - Dot Net Perls
January 24, 2024 - To compute a ceiling, we invoke Math.ceil. This method receives, and returns, a double value.
🌐
Sololearn
sololearn.com › en › Discuss › 2238173 › what-is-mathfloor-and-mathceil-in-java
what is Math.floor and Math.ceil in java? | Sololearn: Learn to code for FREE!
java · 12th Apr 2020, 8:04 AM · Muhammad Asad · 2 Answers · Answer · + 6 · well if you ceil a float, it will result in the next integer of that float a d if you floor that it will tesult in the previous integer.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › ceil
Java Math ceil() - Round Up Value | Vultr Docs
September 27, 2024 - The Math.ceil() method in Java is a widely used function for rounding up numerical values to the nearest larger integer.
🌐
Educative
educative.io › answers › what-is-mathceil-in-java
What is Math.ceil() in Java?
The ceil() function returns the nearest smallest double value that is greater than or equal to a number. Figure 1 shows the mathematical representation of the ceil() function.
🌐
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 - Math.ceil() rounds up to the next highest integer, while Math.floor() rounds down to the next lowest integer. The Math.round() is used for rounding floating-point numbers to the nearest integer, following the “round half up” rule.
🌐
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.
🌐
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...
🌐
CodeAhoy
codeahoy.com › java › Math-Ceil-method-JI_15
Java Math.ceil() Method with Examples | CodeAhoy
October 12, 2019 - We use it to find the ceiling value of a number. This function returns the largest integer greater than or equal to the argument provided. In other words, it returns the next largest integer value of the specified number. For example, if you pass it 3.4, it will return 4. ... It takes an argument ...