# Problem with division using doubles

• 10-10-2009, 07:31 PM
chrismanahan
Problem with division using doubles
Hey guys,
I'm working on a project in my beginner Java class and the only thing that is not working is division. I feel like it might just be something really simple I can't see, but every time I divide two numbers and assign it to a double, the answer truncates itself like an int and loses all values that aren't whole numbers. Here for example, is a program that converts fahrenheit to celsius:

Code:

```public class test {         public static void main(String [] args)         {                 double c;                 double boil = 212;                 String output;                 c = (boil - 32) * (5/9);                 output = boil + " in Fahrenheit is " + c + " in Celsius.";                 System.out.println(output);       } }```
This outputs c = 0 for me because the problem is the 5/9. Instead of being .556, it truncates and is 0.

This code below doe the same thing except instead of using (5/9), I assign the values 5 and 9 to two double vars called one and two, divide, and get the correct answer.
Code:

```public class test {         public static void main(String [] args)         {                 double one, two;                 double c;                 double boil = 212;                 String output;                 one = 5;                 two = 9;                 c = (boil - 32) * (one / two);                 output = boil + " in Fahrenheit is " + c + " in Celsius.";                 System.out.println(output);       } }```
Please help me out! I worked on this for 4 hours last night and couldn't figure out what is wrong
• 10-10-2009, 08:44 PM
Fubarable
make it 5.0/9.0 so that you don't inadvertently do int division here.
• 10-10-2009, 08:48 PM
chrismanahan
Oh my god thanks dude. You're a savior
• 10-10-2009, 09:26 PM
Fubarable
Quote:

Originally Posted by chrismanahan
Oh my god thanks dude. You're a savior

No, I'm just one who has tripped over this problem himself in the past. Good luck.