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
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 โ€บ home โ€บ java/lang โ€บ java math ceil() method
Java Math ceil() Method
September 1, 2008 - Following is the declaration for java.lang.Math.ceil() method ... This method returns the smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer. ... The following example shows the usage of Math ceil() method.
Discussions

Java rounding up to an int using Math.ceil - Stack Overflow
I'm sure there's a more mathematically elegant way to explain it. ... And the result of (157/32.) will be real too. ;) ... Here you should multiply Numerator with 1.0, then it will give your answer. ... Java provides only floor division / by default. But we can write ceiling in terms of floor. More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
Explain the following Math functions in Java: Math.ceil()
Returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integer. For example, Math.ceil(65.5) will return 66.0. More on knowledgeboat.com
๐ŸŒ knowledgeboat.com
1
3
October 14, 2020
Python math.ceil BUG in leetcode
Did the same mistake in one of my onsites ๐Ÿ˜‚. Donโ€™t know how ceil works in python. In java we need to explicitly typecast it to double while calling ceil. 3/6 goes as integer division, math.ceil(3/6) is basically math.ceil(0), so itโ€™s returning 0.0. While Math.ceil(3/(double)6) return 1.0. More on reddit.com
๐ŸŒ r/leetcode
6
1
January 16, 2025
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ math-ceil-java
Java - Math.ceil Method - Dot Net Perls
January 24, 2024 - 1.0, Floor = 1.0, Ceil = 1.0, Equal = true 1.1, Floor = 1.0, Ceil = 2.0, Equal = false 1.5, Floor = 1.0, Ceil = 2.0, Equal = false 1.9, Floor = 1.0, Ceil = 2.0, Equal = false 2.0, Floor = 2.0, Ceil = 2.0, Equal = true ยท With the Math class, we gain many powerful and well-tested methods.
๐ŸŒ
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;
๐ŸŒ
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? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java โ€” a ...
๐ŸŒ
CodeAhoy
codeahoy.com โ€บ java โ€บ Math-Ceil-method-JI_15
Java Math.ceil() Method with Examples | CodeAhoy
October 12, 2019 - The following example illustrates how to use Math.ceil(double) method.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ math.ceil() in java
Math.ceil() in Java - Scaler Topics
May 5, 2024 - If the input parameter is less than 0 but greater than -1, ceil function will return -0.0 as output. ... Math.ceil() take a double value as a parameter.
Find elsewhere
๐ŸŒ
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).
๐ŸŒ
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, ...
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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-ceil-method-examples
Java ceil() method with Examples - GeeksforGeeks
January 21, 2026 - This method belongs to the java.lang.Math class. ... class GFG { public static void main(String[] args) { double num = 2.3; System.out.println(Math.ceil(num)); } }
๐ŸŒ
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 Math.ceil() method is primarily used when we want to ensure that a number is rounded up to the next highest integer, regardless of its decimal part. In the following example, Math.ceil(4.3) rounds up 4.3 to 5.0, ensuring the result is not ...
๐ŸŒ
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.

๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-math-ceil-method
Java Math.ceil() Method
October 9, 2022 - public class JavaExample { public static void main(String[] args) { // -1 < n < 0 double n = -.15; System.out.println(Math.ceil(n)); } }
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ java tutorials โ€บ java math โ€“ ceil() floor() methods
Java Math โ€“ ceil() Floor() Methods
September 20, 2024 - Below is the example of math.round ... Math.ceil and Math.floor in Java methods are used to return the smallest and largest integer that are greater than or equal to the argument....
๐ŸŒ
LogicBig
logicbig.com โ€บ how-to โ€บ code-snippets โ€บ jcode-java-math-ceil.html
Java - Math.ceil() Examples
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 ยท How to use Math.ceil() to round a number up to n decimal places
๐ŸŒ
Java Guides
javaguides.net โ€บ 2023 โ€บ 09 โ€บ java-math-ceil-example.html
Java Math ceil() example
September 26, 2023 - public class CeilExample { public static void main(String[] args) { double[] values = {42.1, 42.7, -42.1, -42.7}; for (double value : values) { // Calculate the ceiling value double ceilValue = Math.ceil(value); System.out.println("Ceiling value ...
๐ŸŒ
Sarthaks eConnect
sarthaks.com โ€บ 3497196 โ€บ explain-java-math-ceil-method
Explain Java Math ceil() Method - Sarthaks eConnect | Largest Online Education Community
April 29, 2023 - LIVE Course for free ยท The ceil() method in Java is a static method of the java.lang.Math class, used to round up a given numerical value to the nearest integer that is greater than or equal to the value passed to the method
๐ŸŒ
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, ...