Math.ceil and Math.floor returning same value

前端 未结 5 712
萌比男神i
萌比男神i 2020-12-31 00:31

I have a double (23.46)

And using the methods Math.ceil and Math.floor and parsing my double to these methods, I get the same value returned to me, which is 23...

5条回答
  •  既然无缘
    2020-12-31 01:33

    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.)

提交回复
热议问题