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
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.

🌐
Programiz
programiz.com › java-programming › library › math › ceil
Java Math ceil()
That is, the value 3.24 will be rounded to 4.0 which is equal to integer 4. class Main { public static void main(String[] args) { double a = 3.24; System.out.println(Math.ceil(a)); } } // Output: 4.0
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_ceil.htm
Java - Math ceil(double) Method
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.
🌐
W3Schools
w3schools.com › java › ref_math_ceil.asp
Java Math ceil() Method
Tip: To round a number to the nearest integer in either direction, look at the round() method. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Codecademy
codecademy.com › docs › java › math methods › .ceil()
Java | Math Methods | .ceil() | Codecademy
December 12, 2022 - The result is the same as the argument if the argument value is already equal to a mathematical integer. The result is identical to the argument if the argument is NaN, +/- infinity, or +/- zero. The parameter num is of type double. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-ceil-method-examples
Java ceil() method with Examples - GeeksforGeeks
April 7, 2018 - 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.
🌐
CodeGym
codegym.cc › java blog › java math › java math.ceil() method
Java Math.ceil() method
October 11, 2023 - That we can freely use by passing ... to the use cases. If the parameter “double” is also a mathematical “integer” [eg: 2.0 is the same as 2] - The result is equal to the integer [i-e; 2 itself]....
🌐
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) ...
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 78549 › in-the-math-class-math-ceil-will-always-return-an-integer-value-then-why-is-the-following-code
In the Math class , Math.ceil() will always return an integer value then why is the following code giving an error ? | Sololearn: Learn to code for FREE!
If you would like to have the ceiling value be an integer, you can cast the expression: int c = (int) (Math.ceil(7.342)); This casting truncates the .0 at the end of the ceiling value Originally, it would have been 8.0; after casting it as an ...
🌐
Scaler
scaler.com › home › topics › math.ceil() in java
Math.ceil() in Java - Scaler Topics
May 5, 2024 - The ceil() is a function of the Math class present in java.lang package. Math.ceil() rounds up a double or float number to the nearest Mathematical integer greater than or equal to the given floating-point number.
🌐
Guru99
guru99.com › home › java tutorials › java math – ceil() floor() methods
Java Math – ceil() Floor() Methods
September 20, 2024 - Math.round() method in Java returns the closed int or long as per the argument. Below is the example of math.round Java method. public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("Round off for d1: " + Math.round(d1)); System.out.println("Round off for d2: " + Math.round(d2)); } } ... The 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.
🌐
TutorialKart
tutorialkart.com › java › java-math › java-math-ceil
Java Math.ceil() - Examples
November 23, 2020 - Since the definition of ceil() function has double datatype as argument, you can pass int, float or long as arguments; because these datatypes will implicitly promote to double. In the following example, we pass a double value as argument to ...
🌐
CodeAhoy
codeahoy.com › java › Math-Ceil-method-JI_15
Java Math.ceil() Method with Examples | CodeAhoy
October 12, 2019 - It takes an argument of type double and returns a value as a double that is less than or equal to the argument and is equal to a mathematical integer. ... The following example illustrates how to use Math.ceil(double) method.
🌐
Bbminfo
bbminfo.com › java › library › math › ceil.php
Java ceil() - Java Math Functions - bbminfo
The Java Math.ceil() function returns the next highest integer value by rounding up value of a floating poing number if necessary.
🌐
Tutorial Gateway
tutorialgateway.org › java-ceil-function
Java ceil Function
March 25, 2025 - The following ceil function will accept positive or negative double value as an argument. It returns the smallest integer value, greater than or equal to the specified expression or Value and equal to a mathematical integer.
🌐
Coderanch
coderanch.com › t › 548975 › java › Java-Math-Floor-Ceil-returning
Java Math - Floor/Ceil returning double (Java in General forum at Coderanch)
Such large double precision numbers can't have a fractional part, so floor(...) or ceiling(...) does nothing. luck, db There are no new questions, but there may be new answers. ... I know what Scott is trying to say. But you missed what I was trying to say -- I can tell that because you used the phrase "fractional part". That's the decimal assumption again. Double values are binary, not decimal. I predict that you will find that doesn't produce an integer.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-ceil-method
Java Math.ceil() Method
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)); } } ... If the given value is an integer, then it returns the same value. If the given value is NaN (Not a number) or an infinity ...