简体   繁体   中英

Math.ceil and Math.floor returning same value

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

I want it to be rounded off to 24.. In otherwords, if I have a double that's 15.01, it should still be rounded off to 16... How do I do this?

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

The code

System.out.println(Math.ceil(23.46));
System.out.println(Math.floor(23.46));

gives me the following output:

24.0
23.0

So it works as expected.

When I run

System.out.println(Math.ceil(23.46));
System.out.println(Math.ceil(15.01));

I get

24.0
16.0

Isn't that what you want?

How do you do the actual invocation? I cannot replicate your result, using either the double object or the primitive type.

This code:

    Double d_object = new Double(23.46);
    double d_simple = 23.46;

    System.out.println("Ceiling simple: " + Math.ceil(d_simple));
    System.out.println("Floor simple: " + Math.floor(d_simple));

    System.out.println("Ceiling object: " + Math.ceil(d_object));
    System.out.println("Floor object: " + Math.floor(d_object));

gives me:

Ceiling simple: 24.0
Floor simple: 23.0
Ceiling object: 24.0
Floor object: 23.0
int num1 ,num2;
Scanner scan = new Scanner(System.in);
          
num1 = scan.nextInt();
num2 = scan.nextInt();

int result = num1 / num2;
           
System.out.println("floor "+num1+" / "+num2+" = "+Math.floor(result));
System.out.println("ceil "+num1+" / "+num2+" = "+Math.ceil(result));
System.out.println("round "+num1+" / "+num2+" = "+Math.round(result));

In my case I was dividing int and then applying ceil which was returning weird results like above.

Eg

Math.ceil(5/2)  // gives 2
Math.floor(5/2) // gives 2

Solve is to use double while dividing, always try casting to double/float in divisions

Math.ceil(5/(double)2)  // gives 3
Math.floor(5/(double)2) // gives 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM