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

Converting textbox string to float?

本文关键字:转换 字符串 文本      更新时间:2023-10-16

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

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

然而,我得到这些错误:

f:microsoft visual studio 9.0projectshellowinhellowinForm1.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.0projectshellowinhellowinForm1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type
f:microsoft visual studio 9.0projectshellowinhellowinForm1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'

请帮助我疯了,它是多么荒谬的困难,从一个文本框在c++中获得一些输入。我已经用谷歌搜索了我遇到的所有错误,但没有任何有用的东西出现,我已经搜索了一个小时的答案,请帮助。

为您修复,

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

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

您正在尝试将字符串与双精度数相乘,并且没有操作符定义如何这样做。您需要首先将字符串转换为双精度类型,然后在计算中使用它。

然后,你试图将字符串赋值给浮点数,这也是无稽之谈。你需要计算浮点数,然后在赋值给文本框文本字段时将其转换为字符串。

类似:

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