Temperature Converter Problems

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Nov 2005
Posts
1

## Temperature Converter Problems

Hi everyone,

I'm sure that this will be a relatively simple questions for you experts, but I'm thoroughly confused trying to code this program. I have been assigned in school to create a temperature converter that converts celsius to farenheit and vice versa. I tried searching for code on the net, but none of the examples that I found use the same formula that I'm required to use. I must use the following formulae:
°F = (40 + °C)*9/5 - 40 and
°C = (40 + °F)*5/9 - 40

This is the code that I'm trying to insert this formula into:
___________________________________________________________
<INPUT TYPE = "text"
NAME = "cels"
VALUE = "0"
SIZE = "3"
MAXLENGTH = "3"
onChange = "fahr.value =
Math.round(40 + this.value * 9.0/5.0 - 40);"> &#176; <STRONG>C</STRONG>
&nbsp; <STRONG>equals</STRONG> &nbsp;
<INPUT TYPE = "text"
NAME = "fahr"
VALUE = "32"
SIZE = "3"
MAXLENGTH = "3"
onChange = "cels.value =
Math.round(40 + this.value * 5.0/9.0 - 40);">
&#176; <STRONG>F</STRONG>*
_____________________________________________________________

The outputs are not correct, and I just can't figure out how to arrange this formula so that it works. Any help that you could give me would be greatly appreciated!

Thanks,
Matt

2. Senior Member
Join Date
Mar 2004
Posts
635
Your problem here is order of operations. 40 has to be added to the degrees first, then that total is multiplied by the fraction. Use a parethesis as used in the equation example at the top.

Math.round((40 + this.value) * 1.8 - 40)
Math.round((40 + this.value) * (5/9) - 40)

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise