1. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
117
Rep Power
0

## Strange Error

Hello!

My program is supposed to convert degrees Fahrenheit to degrees Celsius, but somehow it always outputs 0, and I don't know why. Maybe you do?

Java Code:
import javax.swing.*;

public class attakomma1{
public static void main(String[] args){
String s = JOptionPane.showInputDialog("Enter a temperature in degrees Fahrenheit");
double temperatur = Double.parseDouble(s);
temperatur = (temperatur-32)*(5/9);
JOptionPane.showMessageDialog(null, "Here is the temperature in degrees Celsius: " + temperatur);
}
}

2. ## Re: Strange Error

problem is in 5/9
it will give answer as 0, since it is integer division..
you can solve it by replacing that statement by

temperatur = (temperatur-32)*((double)5/(double)9);

3. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
117
Rep Power
0

## Re: Strange Error

Oh, thanks :) Isn't just writing (double)5/9 sufficient?

4. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## Re: Strange Error

Why not just write 5/9.0

Regards,
Jim

5. ## Re: Strange Error

Isn't just writing (double)5/9 sufficient?
No. What you get there is 5.0/9 which is a double divided by an integer which becomes integer division.

5/9.0
that Jim suggested is integer divided by float, which will result in a float point division. When in doubt, cast everything to the type you expect to work with - but understanding when and how integer division works in java is helpful.

6. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,701
Rep Power
4

## Re: Strange Error

Uh, actually (double)5/9 or 5.0/9 also works.

Regards,
Jim

7. ## Re: Strange Error

5.0 / 9

The 9 will be promoted to a double before the division is performed. Thus giving a double result. Java will never lose precision unless you tell it via a cast or some other truncating method.

8. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,624
Rep Power
3

## Re: Strange Error

Originally Posted by Kuttappu
It wont work. Answer for it will be 0.0
Did you try it, or are you just assuming? Because I tried it, and it works just fine. You are assuming here that the cast will be for the result of the equation, but it is in fact casting the 5 into a double 5.0.

9. ## Re: Strange Error

No. What you get there is 5.0/9 which is a double divided by an integer which becomes integer division.

that Jim suggested is integer divided by float, which will result in a float point division. When in doubt, cast everything to the type you expect to work with - but understanding when and how integer division works in java is helpful.

both will work actually. you just need one of the operands to be in double, doesnt matter if it is 5.0/9 or 5/9.0

10. ## Re: Strange Error

Originally Posted by gimbal2
Did you try it, or are you just assuming? Because I tried it, and it works just fine. You are assuming here that the cast will be for the result of the equation, but it is in fact casting the 5 into a double 5.0.
yes. u are right, it works.. I tried like (double)(5/9).. My bad..

11. ## Re: Strange Error

Oh whoops - good catch. Guess I should try things before talking about them aye? Good life lesson in that :D

12. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,624
Rep Power
3

## Re: Strange Error

Oh whoops - good catch. Guess I should try things before talking about them aye? Good life lesson in that :D
Don't beat yourself up. Being a 64 bit quadcore processor, you have a specialized embedded chip with matching instruction set to handle that stuff for you.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•