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 › home › java/lang › java math ceil() method
Java Math ceil() Method
September 1, 2008 - Learn how to use the Java Math ceil() method to round up numbers to the nearest integer. Explore examples and explanations in this comprehensive guide.
🌐
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 is called with number as the argument. The method rounds the value upward to the nearest integer.
🌐
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
🌐
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.
🌐
CodeGym
codegym.cc › java blog › java math › java math.ceil() method
Java Math.ceil() method
October 11, 2023 - What is the ceil function in mathematics? “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...
🌐
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 ...
🌐
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.
🌐
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 ...
🌐
Tutorialspoint
tutorialspoint.com › home › java › java number ceil
Java Number Ceil
September 1, 2008 - This method returns the smallest integer that is greater than or equal to the argument. Returned as a double. public class Test { public static void main(String args[]) { double d = -100.675; float f = -90; System.out.println(Math.ceil(d)); ...
🌐
Bbminfo
bbminfo.com › java › library › math › ceil.php
Java ceil() - Java Math Functions - bbminfo
The Math.ceil() function in Java is used to return the next highest integer value by rounding up value if necessary.
🌐
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....
🌐
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 ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Math ceil(), floor(), round() Methods - Java Code Geeks
January 3, 2024 - The Math.round() method in Java is commonly used for rounding a number to the nearest integer. It uses the “round half to even” strategy, also known as “bankers’ rounding,” where values equidistant to the two nearest integers are rounded ...