# Yet another temp. converter c# (math problem?)

Page 1 of 1

## 3 Replies - 436 Views - Last Post: 25 July 2013 - 05:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=325548&amp;s=3fbbfe90bf9a90816855e3246154bfb0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kwontum

Reputation: -20
• Posts: 19
• Joined: 24-July 13

# Yet another temp. converter c# (math problem?)

Posted 24 July 2013 - 03:00 PM

So ive tried as many variations of math as i can figure out/locate on google.
I am pretty sure my math is correct, but for some reason my variable seems to be holding old values?
If some one would be so kind enough to run this code they will see.
My first few conversions work great ie; 0c = -32f, 212f = 100c
How ever 100c = 68f is definatley wrong
Any help would be appreciated. /> />
```
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btn1_Click(object sender, EventArgs e) //convert to F
{
_num1 = Convert.ToSingle(txtc.Text);
_answer = (_num1-32) * (9 / 5);

}

private void button1_Click(object sender, EventArgs e) //convert to C
{
_num2 = Convert.ToSingle(txtf.Text);
answer = (_num2 - 32) * 5 / 9; ;
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Yet another temp. converter c# (math problem?)

### #2 Martyr2

• Programming Theoretician

Reputation: 4625
• Posts: 12,677
• Joined: 18-April 07

## Re: Yet another temp. converter c# (math problem?)

Posted 24 July 2013 - 03:18 PM

1) No need to prefix your variables with the underscore. That was an old C convention.
2) If you are going to convert something ToSingle, then the variable you use should be a Single, not float.
3) You have a double semicolon in your conversion to Celsius function there.

Now for your conversion to Fahrenheit, use ((num1 * 9) / 5) + 32;

Enjoy!

### #3 kwontum

Reputation: -20
• Posts: 19
• Joined: 24-July 13

## Re: Yet another temp. converter c# (math problem?)

Posted 24 July 2013 - 03:25 PM

Martyr2, on 24 July 2013 - 03:18 PM, said:

1) No need to prefix your variables with the underscore. That was an old C convention.
2) If you are going to convert something ToSingle, then the variable you use should be a Single, not float.
3) You have a double semicolon in your conversion to Celsius function there.

Now for your conversion to Fahrenheit, use ((num1 * 9) / 5) + 32;

Enjoy! />

re: 1) i was trying to rename my variables to my code easier to read ie num1 = Cent num2 Fah
2) thank you that helps with a seperate problem
3) dont know how that got there

and last but not least thank you for the math turns out thats all that was really wrong

### #4 Charles:)

• D.I.C Regular

Reputation: 148
• Posts: 359
• Joined: 26-November 09

## Re: Yet another temp. converter c# (math problem?)

Posted 25 July 2013 - 05:52 AM

Martyr2, on 24 July 2013 - 04:18 PM, said:

If you are going to convert something ToSingle, then the variable you use should be a Single, not float.

In C# float is just an alias for System.Single

http://msdn.microsof...a(v=vs.71).aspx