2

I'm beginning in Java, and could anyone explain me why Java gives me these answers?

I have a very simple class trying to learn how to round a number. I want 2 decimals so...

public static void main(String[] args) {

    double pi1 = Math.PI;
    System.out.println("pi1 = " + pi1);
    double p2 ;

    p2= Math.round(pi1*100)/100;

    //p2= Math.round(pi1*100)
    //p2=p2/100;

    System.out.println("p2 = " + p2);

}

If I run this result is:

p2 = 3.0

Then I change

    //p2= Math.round(pi1*100)/100;

    p2 = Math.round(pi1*100);
    p2 = p2/100;

Now, result is:

p2 = 3.14

as I wanted

Why with these differences? Why the first option doesn't give me 3.14 I think that I've made a correct code with 1st option.

Please, anyone could tell me why? These things makes me don't trust Java.

Thank you.

GameO7er
  • 2,028
  • 1
  • 18
  • 33
Fer
  • 23
  • 2
  • I know this might not directly solve your problem, but if you haven't yet, read [this](https://stackoverflow.com/questions/37795248/integer-division-in-java). – Sweeper Dec 23 '19 at 10:25

2 Answers2

2

I will assume that you know how integer division works in Java. In short, when both sides of / are integral types, like in 314 / 100, the expression evaluates to an integer too, like 3.

Math.round returns a long, which is an integral type. In your first code, you have the expression Math.round(pi1*100)/100. Math.round(...) returns an integer type, 100 is an integer literal, so integer division occurs.

However, in the second code, you first assigned the result of Math.round to p2. The long returned is implicitly converted to a double first, and stored in p2. You then wrote an expression in p2: p2/100. Here, one of the operands is double, so integer division does not occur.

Therefore, the one liner version that is the same as the second code is:

p2 = ((double)Math.round(pi1*100))/100;

You don't see the double conversion in the second code because it is done implicitly.

A note on rounding

This way rounding doubles should be used if you want to do calculations with the rounded number afterwards. If you just want to display a rounded number as output, you should use String.format, System.out.printf, or DecimalFormat. Read more about these methods here.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thak you very much. I assumed that as I have declared p2 as double, the result, if posible, should be double. – Fer Dec 23 '19 at 10:49
  • @Fer Yeah, I can see where you could have misunderstood, but Java doesn't infer what you want based on the variable you are assigning to. – Sweeper Dec 23 '19 at 10:51
  • Thanks again, and I tried with another option and works p2 = Math.round(pi1*100)/100.0; So, if I want double numbers, I have to use at least one double number – Fer Dec 23 '19 at 10:55
0

No mistake here, it works properly. Method Math.round(double) returns type long.

  • In your first variant, you receive result long 314 and divide it by 100 and get the result 3, and then assign it back to double.

  • In your second variant, you receive long result 314 and assign it back to double. Dividing double keeps precision as opposed to dividing long, so you get the correct result of 3.14

There for make p2= Math.round(pi1*100)/100; as

p2= Math.round(pi1*100) / 100.0;

Output -:

pi1 = 3.141592653589793
p2 = 3.14
Kalana
  • 5,631
  • 7
  • 30
  • 51
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36