-5
System.out.println((double)(20/200)*100);

I thought it was a rounding issue initially but the issue still persists when it's a double.

2 Answers2

2

This question is basically a duplicate, but since you seem to be on the right track, here is one slight change you can do to make this work:

System.out.println((20.0d/200)*100);

That is, make the ratio involve at least one double in the numerator or denominator. After that point, you will have a double for the quotient, and multiplying by an integer will not change that precision.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
2

Because 20/200 is integer divison, hence it will truncate to 0 and the whole expression will return 0.

You should change to float division and to do that is enough to transofrm one of the operands to float type (ex: 20 / 200.0).

NiVeR
  • 8,872
  • 4
  • 26
  • 34