C#中的数据转换问题 [英] data conversion problem in C#

查看:69
本文介绍了C#中的数据转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我在C#Windows平台上工作,我必须将两个文本框值相乘,该值存储在下面的double数据类型变量中,我给出代码

Hi All
I am working in C# windows platform where I have to multiply two text box value which is store in a double data type variable below I m give the code

double a=0.00;
//Calculation for Amount
if (txtItemCode.Text != "")
{
     a = float.Parse(txtItemQty.Text);
}
else
{
    double m = double.Parse(txtItemQty.Text);
    double n = double.Parse(txtPrice.Text);

    a = float.Parse(m * n); //here conversion problem started
  //Error: float.Parse(String)has some invalid arguments
}



请帮助我解决问题

感谢所有
Indrajit



Please help me to solve the issue

Thanks to All
Indrajit

推荐答案

正如您的错误所解释,parse()需要一个字符串.您读过API吗?

您是否尝试过诸如

As your error explains, parse() expecting a string. Did you read the API?

Did you tried anything such as ,

a = m * n;


我在遵循您的业务逻辑时遇到了麻烦.首先,将变量a声明为双精度型.然后测试txtItemQty值以查看其是否为空白,如果为空,则立即将类型重铸为float并将其值指定为0.00.稍后,您清楚地表明您的意思是变量mtxtItemQty的值保存为double;为什么不一致?

让我们用另一种方式做:

I''m having trouble following your business logic. You start out by declaring a variable a as a double. Then you test the txtItemQty value to see if it is blank, and if it is, you immediately recast the type to float and assign it the value of 0.00. Later you clearly indicate that you mean the variable m to hold the value of txtItemQty as a double; why not be consistent?

Let''s do this another way:

if (txtItemQty.Text != "")
   {
      double m = double.Parse(txtItemQty.Text);
      double n = double.Parse(txtPrice.Text);
      double k = m*n;
      MessageBox.Show("Your cost is " & k.ToString());
   }
else
   {
      MessageBox.Show("Please enter a quantity and retry.");
   }


float.Parse()接收,字符串作为参数通过这种方式传递变量:

float.Parse() receive, string as argument pass the variable this way:

double a = 0.00;

          if (txtItemQty.Text != "")
          {
              a = float.Parse(textBox1.Text);
          }
          else
          {
              double m = double.Parse(txtItemQty.Text);
              double n = double.Parse(txtPrice.Text);
              double k = m * n;

              a = float.Parse(k.ToString()); //here conversion problem started
              //Error: float.Parse(String)has some invalid arguments

              MessageBox.Show(a.ToString());


这篇关于C#中的数据转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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