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 OverflowVideos
math - Java - Misunderstand ceil and floor methods - Stack Overflow
Why floor, round and ceil return double?
java - Math.ceil and Math.floor returning same value - Stack Overflow
binary search ceiling and floor of number
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.
Unable to reproduce:
public class Test
{
public static void main(String[] args)
{
System.out.println(Math.ceil(23.46)); // Prints 24
System.out.println(Math.floor(23.46)); // Prints 23
}
}
I suspect that either you haven't got the input data you think you have or you're not writing out the output data you think you are. Math.floor/ceil themselves work fine. The only time they will return the same value is when the input is already an integer. You talk about parsing your double... my guess is that the error lies there. Please show us a short but complete program which demonstrates the problem.
(There may be other scenarios around very large values where the exact target integer can't be represented exactly as a double - I haven't checked - but that's certainly not the case here.)
float x = ((float)2346/100); // You should type cast. Otherwise results 23
Math.ceil(x); // So Math.ceil(23) is 23 !!!
// Here I type cast to float.
// So I get the result 24