Converting textbox string to float?

后端 未结 2 584
臣服心动
臣服心动 2021-01-22 12:28

I\'m basically trying to write a basic converter in visual studio 2008, and I have 2 text boxes, one which gets input from the user, and one which gives output with the result.

相关标签:
2条回答
  • 2021-01-22 12:46

    Fixing it for you,

             String^ i1 = textBox1->Text;
             float rez = (float)(Convert::ToDouble(i1)*4.35);
             textBox2->Text = rez.ToString();
    

    Basically, you want to convert your string to an actual number, do the math, and then make it back into a string for displaying purposes.

    0 讨论(0)
  • 2021-01-22 12:56

    You're trying to multiply a string by a double and there is no operator that defines how to do that. You need to convert your string to a double first, and then use that in the calculation.

    Then, you're trying to assign a string to a float, which again is nonsense.. You need to calculate the float, then convert it to a string when assigning it to the textbox text field.

    Something like:

    String^ i1 = textBox1->Text;
    float rez = (Convert::ToDouble(i1)*4.35);
    textBox2->Text = rez.ToString();
    
    0 讨论(0)
提交回复
热议问题