将文本框字符串转换为浮点数?

时间:2012-01-03 20:14:41

标签: c++ textbox c++-cli

我基本上试图在visual studio 2008中编写一个基本的转换器,我有两个文本框,一个用户输入,另一个用输出结果输出。当我按下按钮时,我希望第一个文本框中的输入乘以4.35然后显示在第二个文本框中。到目前为止,这是我在按钮代码中的代码:

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

但是我收到了这些错误:

f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2676: binary '*' : 'System::String ^' does not define this operator or a conversion to a type acceptable to the predefined operator
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'

请帮助我疯狂地从C ++中的文本框中获取一些输入是多么荒谬。我用谷歌搜索了我的每一个错误,没有任何有用的信息,我已经找了一个小时的答案了,请帮忙。

2 个答案:

答案 0 :(得分:7)

为您修复,

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

基本上,您希望将字符串转换为实际数字,进行数学运算,然后将其重新转换为字符串以便显示。

答案 1 :(得分:1)

您尝试将字符串乘以double,并且没有运算符定义如何执行此操作。您需要先将字符串转换为double,然后在计算中使用它。

然后,你试图将一个字符串分配给一个浮点数,这也是无意义的。你需要计算浮点数,然后在将它分配给文本框文本字段时将其转换为字符串。

类似的东西:

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