乘以值时,输入字符串的格式不正确 [英] Input string was not in a correct format when multiplying values
问题描述
在将标签文本转换为整数并乘以时,输入字符串不正确...
我尝试过:
private void GrandTotal()
{
int GTotal = 0 ;
for ( int i = 0 ; i < BillingGridView.Rows.Count; i ++)
{
String total =(BillingGridView.Rows [i] .Cells [ 7 ]。Text);
GTotal + = Convert.ToInt32(total.ToString());
}
BillTotalLabel.Text = GTotal.ToString();
if (BillTotalLabel.Text!= )
{
int billtotal = Convert.ToInt32(BillTotalLabel.Text);
int vat = Convert.ToInt32(VATPerLabel.Text.ToString());
int totalaftervat = billtotal * vat / 100;
VATValueLabel.Text = totalaftervat.ToString();
}
}
@NotPoliticallyCorrect指出了你正确的方向 - 其中一个的内容textboxes不是一个整数。
使用Convert.ToInt32
的问题在于它会像你一样发现,如果字符串不是整数,则抛出异常。例如1就可以了,12345就可以,但是
35和14.6都会导致你观察到的失败。
更好的解决方案是使用整数解析方法之一 - 参见如何:将字符串转换为数字(C#编程指南) [ ^ ]
您的代码还有其他问题...
string total =(BillingGridView.Rows [i] .Cells [ 7 ]。Text);为我抛出一个编译错误,它应该是
String total =(BillingGridView.Rows [i] .Cells [ 7 ]。Value.ToString());
行int vat = Convert.ToInt32(VATPerLabel.Text.ToString());在其中有一个不必要的
.ToString()
调用...VATPerLabel.Text
已经是一个字符串!类似地GTotal + = Convert.ToInt32(total.ToString());不需要
.ToString()
因为您已经将总计
声明为字符串
。
行int billtotal = Convert.ToInt32(BillTotalLabel.Text);
有不必要的转换它...你刚刚将BillTotalLabel.Text设置为
GTotal.ToString();
这样该行应该是int billtotal = GTotal;
所以你应该有这样的东西(注意我还没有测试过这个)private void GrandTotal()
{
int GTotal = 0 ;
int res;
for ( int i = 0 ; i < BillingGridView.Rows.Count; i ++)
{
String total =(BillingGridView.Rows [i] .Cells [ 7 ]。Value.ToString());
if ( int .TryParse(total, out res))
GTotal + = Convert.ToInt32(total);
}
BillTotalLabel.Text = GTotal.ToString();
VATValueLabel.Text = ;
if ( String .IsNullOrWhiteSpace(BillTotalLabel.Text))
返回跨度>;
int billtotal = GTotal;
int vat;
if ( int .TryParse(VATPerLabel.Text, out 增值税))
{
int totalaftervat = billtotal * vat / 100;
VATValueLabel.Text = totalaftervat.ToString();
}
}
我会留下最后一条评论 - 我认为你不想使用
int
对于其中一些变量 - 十进制或双精度可能更合适
如果空间或某些角色即将到来,请放置适当的修剪。
get input string was not in correct for when converting label text to integer and multiplying...
What I have tried:
private void GrandTotal()
{
int GTotal = 0;
for (int i = 0; i < BillingGridView.Rows.Count; i++)
{
String total = (BillingGridView.Rows[i].Cells[7].Text);
GTotal += Convert.ToInt32(total.ToString());
}
BillTotalLabel.Text = GTotal.ToString();
if (BillTotalLabel.Text != "")
{
int billtotal = Convert.ToInt32(BillTotalLabel.Text);
int vat = Convert.ToInt32(VATPerLabel.Text.ToString());
int totalaftervat = billtotal * vat/100;
VATValueLabel.Text = totalaftervat.ToString();
}
}
@NotPoliticallyCorrect has pointed you in the right direction - the content of one of the textboxes is not an integer.
The problem with usingConvert.ToInt32
is that it will, as you have discovered, throw an exception if the string is not an integer. For example "1" will be ok, "12345" will be ok but both "
35" and "14.6" will cause the failure you have observed.
A better solution is to use one of the integer parsing methods - see How to: Convert a String to a Number (C# Programming Guide)[^]
There are other issues with your code ...
string total = (BillingGridView.Rows[i].Cells[7].Text);is throwing a compile error for me, it should probably be
String total = (BillingGridView.Rows[i].Cells[7].Value.ToString());
The lineint vat = Convert.ToInt32(VATPerLabel.Text.ToString());has an unnecessary
.ToString()
call in it ...VATPerLabel.Text
is already a string! SimilarlyGTotal += Convert.ToInt32(total.ToString());doesn't need the
.ToString()
as you have already declaredtotal
as aString
.
The lineint billtotal = Convert.ToInt32(BillTotalLabel.Text);
has an unnecessary conversion in it ... you have just set the BillTotalLabel.Text to be
GTotal.ToString();
so that line should beint billtotal = GTotal;
So you should have something like this (note I have not tested this)private void GrandTotal() { int GTotal = 0; int res; for (int i = 0; i < BillingGridView.Rows.Count; i++) { String total = (BillingGridView.Rows[i].Cells[7].Value.ToString()); if(int.TryParse(total, out res)) GTotal += Convert.ToInt32(total); } BillTotalLabel.Text = GTotal.ToString(); VATValueLabel.Text = ""; if (String.IsNullOrWhiteSpace(BillTotalLabel.Text)) return; int billtotal = GTotal; int vat; if (int.TryParse(VATPerLabel.Text, out vat)) { int totalaftervat = billtotal*vat/100; VATValueLabel.Text = totalaftervat.ToString(); } }I will leave you with one final comment - I don't think you want to be using
int
for some of these variables - decimal or double might be more appropriate
Place proper trim if space or some character is coming.
这篇关于乘以值时,输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!