将文本框字符串转换为浮点数? [英] Converting textbox string to float?

查看:42
本文介绍了将文本框字符串转换为浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是在尝试在 Visual Studio 2008 中编写一个基本的转换器,我有 2 个文本框,一个用于获取用户的输入,一个用于输出结果.当我按下按钮时,我希望第一个文本框中的输入乘以 4.35,然后显示在第二个文本框中.到目前为止,这是我在按钮代码中的代码:

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. When I press the button I want the input from the first textbox to multiply by 4.35 then display in the 2nd textbox. This is my code in the button code so far:

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

但是我遇到了这些错误:

However I'm getting these errors:

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++ 中的文本框获取一些输入是多么可笑的困难.我已经用谷歌搜索了我遇到的所有错误,但没有发现任何有用的信息,我已经搜索了一个小时的答案,请帮忙.

Please help I'm going insane on how ridiculously difficult it is to get some input from a textbox in C++. I've googled every error I had and nothing useful came up, I've been searching answers for an hour already, please help.

推荐答案

为你解决,

         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.

这篇关于将文本框字符串转换为浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆