Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer

The key is in the phrase that is less than or equal to the argument.

So 2.0 is the largest double value that is less than or equal to 2.1 that is also equal to an integer value.

Ditto for ceil: the description mentions the smallest value that is larger or equal to the input value...

So, the original descriptions are in fact correct.

Answer from Jason on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_math_ceil.asp
Java Math ceil() Method
System.out.println(Math.ceil(0.60)); ...intln(Math.ceil(-5.1)); System.out.println(Math.ceil(-5.9)); ... The ceil() method rounds a number UP to the nearest integer....
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ java tutorials โ€บ java math โ€“ ceil() floor() methods
Java Math โ€“ ceil() Floor() Methods
September 20, 2024 - Below is the Math floor and ceiling Java example. public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("Ceiling of '" + d1 + "' = " + Math.ceil(d1)); System.out.println("Floor of '" + d1 + "' = " + Math.floor(d1)); System.out.println("Ceiling of '" + d2 + "' = " + Math.ceil(d2)); System.out.println("Floor of '" + d2 + "' = " + Math.floor(d2)); } } We will get the below output of the math.ceil in Java example.
Discussions

math - Java - Misunderstand ceil and floor methods - Stack Overflow
When I read "closest to positive infinity" and "closest to negative infinity" I think in the number line: ... EDIT: What I am asking is: the description broke my mind. My logic says (about floor for example): First, Ok when I listen floor I think in the smallest not in the largest. Second, if I returns the largest, that is greater not less than to the argument. The same happens with ceil... 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
Java rounding up to an int using Math.ceil - Stack Overflow
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: More on stackoverflow.com
๐ŸŒ stackoverflow.com
what is the difference between using math.ceil and round for rounding the decimal ?
ceil always rounds up. More on reddit.com
๐ŸŒ r/learnpython
3
2
October 10, 2022
๐ŸŒ
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 - Math.ceil() rounds up to the next highest integer, while Math.floor() rounds down to the next lowest integer. The Math.round() is used for rounding floating-point numbers to the nearest integer, following the โ€œround half upโ€ rule.
๐ŸŒ
Programzools
programzools.com โ€บ find-ceil-and-floor-in-java.html
Find ceil and floor values in Java - ProgramZools
Floor value is the equivalent integer value by rounding down a float or double to the nearest integer (eg floor of 12.8 is 12). ceil(double a) of Math class can be used to rounding up a double value and floor method can be used to rounding down a double value.
๐ŸŒ
Javacodex
javacodex.com โ€บ Math-Examples โ€บ Floor-and-Ceiling-Example
Java Examples | Math Examples | Floor and Ceiling Example
The method Math.ceil() 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. class FloorCeiling { public static void main(String args[]){ double x = 7.5; double y = -8.4; System.out.println("Ma...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ number_ceil.htm
Java - ceil() Method
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)); ...
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-the-Ceil-Floor-and-Rint-functions-in-Java
What is the use of the Ceil(), Floor, and Rint functions in Java? - Quora
* ceil(x) returns the smallest integer bigger than x. * floor(x) returns the largest integer smaller than x. * rint(x) returns the integer closest to x. If x is a number ending in ...
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ floor-and-ceiling-functions-in-java
Understanding Floor and Ceiling Functions in Java โ€” javaspring.net
The floor and ceiling functions are essential tools for such operations. The floor function rounds a number down to the nearest integer, while the ceiling function rounds a number up to the nearest integer.
๐ŸŒ
Quora
quora.com โ€บ What-are-two-differences-between-Math-ceil-and-Math-floor-in-Java
What are two differences between Math.ceil and Math.floor in Java? - Quora
July 23, 2025 - Answer (1 of 4): The Math.floor and Math.ceil methods give you the nearest integer up or down. The method Math.floor returns the largest Double data type that is less than or equal to the argument and is equal to mathematical integer.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ math โ€บ ceil
Java Math ceil()
Become a certified Java programmer. Try Programiz PRO! ... The ceil() method rounds the specified double value upward and returns it. The rounded value will be equal to the mathematical integer.
๐ŸŒ
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.

๐ŸŒ
YouTube
youtube.com โ€บ watch
Find Ceil & Floor Of An Number In A Sorted Array | FREE DSA Course in JAVA | Lecture 51 - YouTube
In this lecture we have to find the ceil and floor of a number in a sorted array.So the first basic question is - What is a ceil or floor? Let's understandA ...
Published ย  March 5, 2021
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-math-ceil-method
Java Math.ceil() method with Examples - Javatpoint
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 ยท
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ rounding-numbers-with-math-round-math-floor-and-math-ceil-in-java-d201bbeb85e2
Javaโ€™s Math Rounding Methods Explained | Medium
March 7, 2025 - Math.round() applies a half-up rule, shifting values based on a simple addition and truncation process. Math.floor() directly removes the decimal portion and moves toward negative infinity, while Math.ceil() does the opposite, always rounding up.
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java
Java Math ceil(), floor(), round() Methods - Java Code Geeks
October 12, 2019 - 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, Math.floor() for rounding down to the nearest integer, and Math.round() ...
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ math-ceil-java
Java - Math.ceil Method - Dot Net Perls
May 5, 2024 - A floor is the lower integer, and a ceiling is the higher one.
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 โ€บ mathematics โ€บ difference-between-floor-and-ceil-function
Difference Between Floor and Ceil Function - GeeksforGeeks
October 1, 2024 - Solution: floor of 67.212 is integer just lesser than 67.212 that is 67. So, floor of 67.212 is 67. Following is a table of Differences between Floor and Ceil Function: