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));
๐ŸŒ
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;
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-ceil-method-examples
Java ceil() method with Examples - GeeksforGeeks
January 21, 2026 - Example 1: This program demonstrates how the Math.ceil() method works with different types of double values in Java.
๐ŸŒ
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)); } } Let us compile ...
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.

๐ŸŒ
W3Schools
w3schools.in โ€บ java โ€บ numbers
Java Numbers - W3Schools
May 28, 2019 - public class Multi_Example { public static void main(String argu[]) { Integer g = 6; Integer d = 4; Double c = Double.valueOf(5); Float fl = Float.valueOf("62"); System.out.println(g.compareTo(6)); System.out.println(g.compareTo(4)); System.out.println(g.equals(d)); System.out.println(c); System.out.println(fl); Integer h = 22; System.out.println(h.toString()); System.out.println(Integer.toString(22)); Integer val = -5; System.out.println(Math.abs(val)); double val2 = -200.456; System.out.println(Math.ceil(val2)); System.out.println(Math.floor(val2)); } } Program Output: 0 1 false 5.0 62.0 22 22 5 -200.0 -201.0 ยท
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_math.asp
Java Math
For example, Math.pow(2, 8) returns 256.0 (not 256). Java has several methods for rounding numbers: Math.round(x) - rounds to the nearest integer ยท Math.ceil(x) - rounds up (returns the smallest integer greater than or equal to x) Math.floor(x) ...
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-math-ceil-method
Java Math.ceil() method with Examples - Javatpoint
February 7, 2021 - Java CopyOnWriteArrayList ยท indexOf() lastIndexOf() clone() toArray() Math.abs() Math.max() Math.min() Math.round() Math.sqrt() Math.cbrt() Math.pow() Math.signum() Math.ceil() Math.copySign() Math.nextAfter() Math.nextUp() Math.nextDown() Math.floor() Math.floorDiv() Math.random() Math.rint() Math.hypot() Math.ulp() Math.getExponent() Math.IEEEremainder() Math.addExact() Math.subtractExact() Math.multiplyExact() Math.incrementExact() Math.decrementExact() Math.negateExact() Math.toIntExact() Math.log() Math.log10() Math.log1p() Math.exp() Math.expm1() Math.sin() Math.cos() Math.tan() Math.asin() Math.acos() Math.atan() Math.sinh() Math.cosh() Math.tanh() Math.toDegrees ยท
๐ŸŒ
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) { double n1 = 5.0/0; //positive infinity double n2 = -5.0/0; //negative infinity System.out.println(Math.ceil(n1)); System.out.println(Math.ceil(n2)); } }
๐ŸŒ
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....
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_ceil.asp
Python math.ceil() Method
# Import math library import math # Round a number upward to its nearest integer print(math.ceil(1.4)) print(math.ceil(5.3)) print(math.ceil(-5.3)) print(math.ceil(22.6)) print(math.ceil(10.0)) Try it Yourself ยป
๐ŸŒ
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.
๐ŸŒ
CodeAhoy
codeahoy.com โ€บ java โ€บ Math-Ceil-method-JI_15
Java Math.ceil() Method with Examples | CodeAhoy
October 12, 2019 - 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 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.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ tryjava.asp
W3Schools online JAVA editor
The W3Schools online code editor allows you to edit code and view the result in your browser
๐ŸŒ
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, ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_ceil.asp
JavaScript Math ceil() Method
The Math.ceil() method rounds a number rounded UP to the nearest integer.