这段代码给出了输入字符串格式不正确的例外,为什么呢? [英] this code giving exception that input string was not in correct format why ?

查看:86
本文介绍了这段代码给出了输入字符串格式不正确的例外,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void calculation()
       {
           try
           {
              double qty = Convert.ToDouble(textBox1.Text);
             double fat = Convert.ToDouble(textBox3.Text);
              double clr = Convert.ToDouble(textBox4.Text);
             double rate = Convert.ToDouble(textBox6.Text);


               if (radioButton1.Checked==true)
               {
                   if (snf / fat > 1.30)
                   {

                       snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                       textBox5.Text = Convert.ToString(snf);
                       snfrate = Math.Round(rate * (60 / 6.5));
                       fatrate = Math.Round(rate * (40 / 8.5));
                       kgfat = Math.Round(((qty * (fat / 100))), 3);
                       textBox7.Text = kgfat.ToString();
                       kgsnf = Math.Round(((qty * (snf / 100))), 3);
                       textBox8.Text = kgsnf.ToString();
                       amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                       textBox10.Text = amt.ToString();


                   }
                   else
                   {
                       //qty = Convert.ToDouble(textBox1.Text);
                       //fat = Convert.ToDouble(textBox3.Text);
                       //clr = Convert.ToDouble(textBox4.Text);
                       //rate = Convert.ToDouble(textBox6.Text);
                       snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                       textBox5.Text = Convert.ToString(snf);
                       snfrate = Math.Round(rate * (52 / 6.5));
                       fatrate = Math.Round(rate * (48 / 9));
                       kgfat = Math.Round(((qty * (fat / 100))), 3);
                       textBox7.Text = kgfat.ToString();
                       kgsnf = Math.Round(((qty * (snf / 100))), 3);
                       textBox8.Text = kgsnf.ToString();
                       amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                       textBox10.Text = amt.ToString();

                   }

               }
               if (radioButton2.Checked)
               {
                   //qty = Convert.ToDouble(textBox1.Text);
                   //fat = Convert.ToDouble(textBox3.Text);
                   //clr = Convert.ToDouble(textBox4.Text);
                   //rate = Convert.ToDouble(textBox6.Text);
                   snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                   textBox5.Text = Convert.ToString(snf);
                   snfrate = Math.Round(rate * (60 / 6.5));
                   fatrate = Math.Round(rate * (40 / 8.5));
                   kgfat = Math.Round(((qty * (fat / 100))), 3);
                   textBox7.Text = kgfat.ToString();
                   kgsnf = Math.Round(((qty * (snf / 100))), 3);
                   textBox8.Text = kgsnf.ToString();
                   amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                   textBox10.Text = amt.ToString();
               }
               if (radioButton3.Checked)
               {
                   //qty = Convert.ToDouble(textBox1.Text);
                   //fat = Convert.ToDouble(textBox3.Text);
                   //clr = Convert.ToDouble(textBox4.Text);
                   //rate = Convert.ToDouble(textBox6.Text);
                   snf = Math.Round(((fat * .2) + (clr / 4) + .14), 3);
                   textBox5.Text = Convert.ToString(snf);
                   snfrate = Math.Round(rate * (52 / 6.5));
                   fatrate = Math.Round(rate * (48 / 9));
                   kgfat = Math.Round(((qty * (fat / 100))), 3);
                   textBox7.Text = kgfat.ToString();
                   kgsnf = Math.Round(((qty * (snf / 100))), 3);
                   textBox8.Text = kgsnf.ToString();
                   amt = Math.Round(((kgfat * fatrate) + (snfrate * kgsnf)), 3);
                   textBox10.Text = amt.ToString();
               }

           }
           catch (Exception ex)
           { MessageBox.Show(ex.Message); }
       }

推荐答案

请参阅对该问题的评论.异常是可以预料的:它发生在ToDouble中.您通常无法避免这种情况.捕获异常并指导用户如何修复文本框中的输入以使其正确.

修正您的编码样式.永远不要这样命名控件.名称是自动生成的,但这并不意味着您可以保留它们.这违反了Microsoft的命名约定.重命名所有此类名称,以便为所有内容赋予一些语义名称.您认为Visual Studio重构引擎是做什么用的?

-SA
Please see the comments to the question. The exception is quite expectable: it happens in ToDouble. You cannot generally avoid it. Catch the exception and instruct to the user how to fix the input in the text boxes to make it correct.

Fix your coding style. Never name controls like that. The names were auto-generated but it does not mean you can leave them as is; this violate Microsoft naming conventions. Rename all such names to give some semantic names to everything. What do you think Visual Studio re-factoring engine is created for?

—SA


因为您切勿对来自TextBox的用户指定值使用Convert.ToDouble.使用Double.TryParse()代替:

Because you should NEVER use Convert.ToDouble on a user-specified value coming from a TextBox. Use Double.TryParse() instead:

double value1;
if (Double.TryParse(textBox1.Text, out value1))
{
    // it's a valid double, so go ahead and do something with it
}




除此之外,如果我是您,我会使用decimal类型而不是double,因为当您使用decimal值进行数学运算时,您会获得更好的可预测精度.




Beyond that, if I were you, I''d use a decimal type instead of a double because you get MUCH better and predictable precision when you''re doing math with a decimal value.


我的文本框是空的.没有填充文本框计算结果,这就是我得到这个错误的原因.
my textbox was empty.without filling the textbox calculation was happing that''s why i got this error.


这篇关于这段代码给出了输入字符串格式不正确的例外,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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