且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决错误:输入字符串格式不正确

更新时间:2023-09-13 14:57:58

既然你是初学者,我会为你解决这两个问题。是的,一个解决方案。 :-)阅读答案!



1)首先,



异常:

输入字符串的格式不正确



表示要提供的字符串解析为小数,格式不正确。简单的英语应用,在几分之一秒内,任何人都可以理解代码抱怨格式。你有,吗?当您无法将字符串转换为十进制值时,会出现许多其他类似的问题。如果您的语言环境(文化)支持以十进制格式提供123,45,那么请确保您使用的是支持此类格式的文化,而不是默认格式。只是问,这可能是一个麻烦,否则调试它以了解更多。这个MSDN文档将帮助您理解十进制格式以及如何考虑十进制值(当然是在字符串中): https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx [ ^ ]



2)其次,另一个问题是因为你试图将数字除以零。如果你曾经学过数学(我没有主张,你从来没有这样做过),你就会知道除以零的东西是未定义。在编程中,除以零的是 DivideByZeroException [ ^ ]。所以,你需要确保不要将任何东西除以零。如果您愿意,可以更改程序流程。



  //  假设变量和案例 
int a,b,c;
// 获取值

if (b!= 0 ){
c = a / b;
} 其他 {
// DivideByZero将被提升
}





这样,您也可以最小化该错误。现在,我知道我没有提供解决方案,但使用这种通用解决方案,您可以解决问题以及可能再次出现的类似问题。



现在,打开Visual Studio,添加一个断点并调试应用程序 [ ^ ]找出出了什么问题,以及可以做些什么或者做些什么来使它正常工作。


1。学会使用'Decimal.TryParse():你现在使用它的方式:你没有检查结果是'true,还是'false ...这就是你如何避免错误:[ ^ ]。



2.考虑制作TextBox控件,使用accept only Decimal条目。 CodeProject是你的朋友:本文展示了你可以采用的各种方法:[ ^ ]。


这是真的,当然你可以做到,但你可以启发关于这个

>

Hi I have simple app c# I want to do simple math operation by input values textboxes . but because of may need to calculate two operation SIMULTANEOUS then I use 6 textbox in my form some when need calculate only two values and some when 3 or 4.
this my code :

Decimal total;
            Decimal INDICATOR;
            Decimal result;
            total = ((Decimal.Parse(txtyear_price1.Text)) / (Decimal.Parse(txtindex1.Text))) + ((Decimal.Parse(txtyear_price2.Text)) / (Decimal.Parse(txtindex2.Text)));
            INDICATOR = total * (Decimal.Parse(txtnewindex.Text));
            result = (Decimal.Parse(txtnewprice.Text)) - INDICATOR;
            label1.Text = result.ToString("N", CultureInfo.CreateSpecificCulture("es-ES"));



or


private void button1_Click(object sender, EventArgs e)
       {


           Decimal txt1;
           Decimal txt2;
           Decimal txt3;
           Decimal txt4;

           Decimal.TryParse(txtyear_price1.Text.Trim(), out txt1);
           Decimal.TryParse(txtyear_price2.Text.Trim(), out txt2);
           Decimal.TryParse(txtindex1.Text.Trim(), out txt3);
           Decimal.TryParse(txtindex2.Text.Trim(), out txt4);

           Decimal total;
           Decimal INDICATOR;
           Decimal result;
           total = (txt1/txt3) + (txt2/txt4);
           INDICATOR = total * (Decimal.Parse(txtnewindex.Text));
           result = (Decimal.Parse(txtnewprice.Text)) - INDICATOR;
           label1.Text = result.ToString("N", CultureInfo.CreateSpecificCulture("es-ES"));
       }



my problem is that when I fill all of textboxes it work but when give up two of them the error :

Input string was not in a correct format


and the error for second code is :

Attempted to divide by zero.


please help me
It is OBVIOUS that I am beginner .

Since you are a beginner, I will give you a solution for both of these problems. Yes, a solution. :-) Read the answer for that!

1) First of all,

Exception:

Input string was not in a correct format


Means that the string that you are providing to parse to a decimal, is not in a correct format. Simple english applied and within a fraction of second anyone can understand that the code is complaining about the format. Are you having "," in it? Many other similar problems arise, when you cannot convert the string to a decimal value. If your locale (culture) supports to have 123,45 in a decimal format then make sure you are using the culture that supports such format, and not the default ones. Just asking, this may be a trouble, otherwise debug it to find out more. This MSDN documentation would help you in understanding the decimal formats and how you should consider a decimal value to look like (in string of course): https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx[^]

2) Secondly, the other problem is because you are trying to divide the number by a zero. If you ever studied mathematics (I am not claiming, you never did), you do know that something divided by zero is undefined. In programming, something divided by zero is "DivideByZeroException[^]". So, you need to make sure you do not divide anything by zero. If you are about to, you can change the flow of program.

// Hypothetical variables and case
int a, b, c;
// Get the values

if(b != 0) {
   c = a / b;
} else {
   // DivideByZero would be raised
}



This way, you can minimize that error too. Now, I know I didn't provide the solution, but using this general solution you can solve the problem and any problem similar that may arise again.

Now, open Visual Studio, add a breakpoint and debug the application[^] to find out what is going wrong and what may be expected or done to make it work fine.


1. learn to use 'Decimal.TryParse() : the way you use it now: you are not checking if the result is 'true, or 'false ... that's how you avoid errors: [^].

2. Consider making the TextBox Controls you use accept only Decimal entry. CodeProject is your friend: this article shows a variety of ways you could go about that: [^].


It's true, of course you can do that, but you can inspire you about this.