3 Replies - 322 Views - Last Post: 25 July 2013 - 05:52 AM Rate Topic: -----

#1 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • 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. :surrender:/> :surrender:/>
 
namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        private float _num1, _num2, _answer, answer;
        public Form1()
        {
            InitializeComponent();
        }

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

        }

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



Is This A Good Question/Topic? 0
  • +

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

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

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

Posted 24 July 2013 - 03:18 PM

Your formula is just wrong. But before that, a few comments...

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! :)
Was This Post Helpful? 2
  • +
  • -

#3 kwontum  Icon User is offline

  • New D.I.C Head

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

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

Posted 24 July 2013 - 03:25 PM

View PostMartyr2, on 24 July 2013 - 03:18 PM, said:

Your formula is just wrong. But before that, a few comments...

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
Was This Post Helpful? -1
  • +
  • -

#4 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 142
  • View blog
  • Posts: 347
  • Joined: 26-November 09

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

Posted 25 July 2013 - 05:52 AM

View PostMartyr2, 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
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1