Math.ceil() will always round up, however you are doing integer division with 3/2. Thus, since in integer division 3/2 = 1 (not 1.5) the ceiling of 1 is 1.

What you would need to do to achieve the results you want is Math.ceil(3/2.0);

By doing the division by a double amount (2.0), you end up doing floating point division instead of integer division. Thus 3/2.0 = 1.5, and the ceil() of 1.5 is always 2.

Answer from jjnguy 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));
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()
System.out.println(Math.ceil(a)); // 2.0 // value equals to 5 after decimal double b = 1.5;
🌐
Scaler
scaler.com › home › topics › math.ceil() in java
Math.ceil() in Java - Scaler Topics
May 5, 2024 - The ceil function returns a double value, which is equal to the nearest Mathematical integer greater than or equal to the passed parameter.
🌐
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)); } }
🌐
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.
🌐
Untitled Publication
kumaramit1947.hashnode.dev › getting-ceil-value-of-the-integral-division-in-java
Getting Ceil value of the integral division in Java
April 5, 2023 - Ceil value = smallest integer greater than or equal to the number (e.g., ceil(3.6) = 4 ). In JAVA, the division operator / returns the floor value.
🌐
OpenJDK
bugs.openjdk.org › browse › jdk-8273090
Loading...
+ * <p> + * The ceiling modulus is {@code r = x - (ceilDiv(x, y) * y)}, + * has the opposite sign as the divisor {@code y} or is zero, and + * is in the range of {@code -abs(y) < r < +abs(y)}. + * + * <p> + * The relationship between {@code ceilDiv} and {@code ceilMod} is such that: + * <ul> ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-ceil-ab-without-using-ceil-function
Find ceil of a/b without using ceil() function - GeeksforGeeks
September 20, 2022 - // C++ program to find ceil(a/b) // without using ceil() function #include <cmath> #include <iostream> using namespace std; // Driver function int main() { // taking input 1 int a = 4; int b = 3; int val = (a / b) + ((a % b) != 0); cout << "The ceiling value of 4/3 is " << val << endl; // example of perfect division // taking input 2 a = 6; b = 3; val = (a / b) + ((a % b) != 0); cout << "The ceiling value of 6/3 is " << val << endl; return 0; } ... // Java program to find // ceil(a/b) without // using ceil() function class GFG { // Driver Code public static void main(String args[]) { // taking
Top answer
1 of 3
28

This is probably because when you divide an Integer by an Integer you get an Integer back which has the same value as Decimal.round(RoundingMode.DOWN).

system.assertEquals(1, 8/5);
system.assertEquals(8/5, (8.0/5).round(RoundingMode.DOWN);

system.assertEquals(-1, -8/5);
system.assertEquals(-8/5, (-8.0/5).round(RoundingMode.DOWN);

system.assertEquals(2, 7/3);
system.assertEquals(7/3, (7.0/3).round(RoundingMode.DOWN);

If you know you have Integers and you want to get their ceiling, you could do something like:

public static Integer ceiling(Integer x, Integer y)
{
    return Math.ceil(Decimal.valueOf(x).divide(y, /*digits*/ 1));
}
2 of 3
17

Integer Division

In Apex Code, similar to Java, when there are two like numeric data types (e.g. both Integers), then they are calculated in a way that returns the same data type. Integers cannot store fractions, so when you do something like 7/3, the fraction is silently discarded. As far as I know, integer division works in the same for every programming language in the world where the result of integer arithmetic is an integer.In many languages where the result of two integers being divided together results in an integer, the fraction is often dropped entirely, although exceptions do exist (some perform rounding, instead).

Parameter Promotion

You'll notice that there's no function Math.ceil that accepts an Integer. This means if you give it an integer, it will implicitly be cast to a floating point value before being processed by Math.ceil.

Arithmetic Promotion

When two numbers are operated on using the standard operators (+, -, /, *), if one of those parameters are a floating point value, the other one will also automatically become a floating point value. Similarly, if a integer and a long were involved in an operation, both numbers become long, and the return type becomes long. So, the goal is to create a situation where a floating point is returned. For example, this results in the correct result:

Integer x = Math.ceil(7.0/3).intValue();

Here, the 7.0 indicates a floating point operation. You'd also get the same effect if you did this:

Decimal x = 7;
Integer y = 3;
Integer z = Math.ceil(x/y).intValue();

This behavior is well-defined, and mimics the behavior in Java. You can read more about automatic widening conversion in the Java documentation, as well as rules about integer division. You'll find that, while not explicitly mentioned in the Apex Code Developer's Guide (as far as I can tell), it obeys the same rules.

🌐
Tutorialspoint
tutorialspoint.com › java › number_ceil.htm
Java - ceil() Method
Java Vs. C++ ... The method ceil gives the smallest integer that is greater than or equal to the argument.
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › floor-and-ceil-of-integer-division
Floor and Ceil of Integer Division - GeeksforGeeks
July 14, 2025 - #include <iostream> #include <vector> using namespace std; // Function to compute floor of a / b int floorDiv(int a, int b) { int q = a / b; // adjust down if signs differ and not divisible if ((a ^ b) < 0 && a % b != 0){ q--; } return q; } // Function to compute ceil of a / b int ceilDiv(int a, int b) { int q = a / b; // adjust up if signs same and not divisible if ((a ^ b) > 0 && a % b != 0){ q++; } return q; } vector<int> divFloorCeil(int a, int b){ vector<int> res; res.push_back(floorDiv(a,b)); res.push_back(ceilDiv(a,b)); return res; } int main() { int a = -7, b = 2; vector<int> res = divFloorCeil(a, b); cout << res[0] << ' ' << res[1] << endl; return 0; } Java ·