0

So I am writing a program in which I am using a Double, but java is not using the decimal point. For example:

I have

double F;

F = 9/5;

Instead of java giving me 1.8 as an answer, it is just giving me back 1.0

Thanks for all the help

Seth
  • 1

2 Answers2

2

Change it to

F = (double)9/5;

because 9/5 will give int which then converted to double giving you 1.0.

singhakash
  • 7,891
  • 6
  • 31
  • 65
0

When you write

F = 9/5;

Java performs the integer division of 9 and 5, which is 1. Because the destination variable F is a float, Java converts the 1 into the equivalent 1.0. If you want it to perform a floating-point division, use 9.0/5. That way, the 5 is promoted to the equivalent 5.0, and the division is thus 9.0/5.0=1.8.

skeggse
  • 6,103
  • 11
  • 57
  • 81