将文本框字符串转换为浮点
Converting textbox string to float?
我基本上试图在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();
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数