user818700
user818700

Reputation:

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?

Upvotes: 22

Views: 92474

Answers (7)

Archit Agarwal
Archit Agarwal

Reputation: 141

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

Upvotes: 0

Ahmed Ramadan
Ahmed Ramadan

Reputation: 210

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

Upvotes: 0

Chris J
Chris J

Reputation: 1029

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

Upvotes: 16

ACEG
ACEG

Reputation: 2039

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

Upvotes: 3

martin
martin

Reputation: 2194

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.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500775

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

Upvotes: 52

Peter Lawrey
Peter Lawrey

Reputation: 533530

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?

Upvotes: 3

Related Questions