C# 输入字符串的格式不正确,出现错误
我得到一个错误: 输入字符串不是corect格式 行中:C# 输入字符串的格式不正确,出现错误,c#,asp.net,C#,Asp.net,我得到一个错误: 输入字符串不是corect格式 行中: int amount = Convert.ToInt32(txtAmount.Text); 您只需确保在文本框中只键入数字,例如1 2 3 如果您键入任何非数字字符,如字母等。。那么它就不能转换成等价的整数。例如字符串:“234”可以转换为整数,但“23A4”不能 以下是3种转换为整数的方法: Int32.Parse(),Convert.ToInt32(),和Int32.TryParse() 在这三个函数中,Int32.TryParse
int amount = Convert.ToInt32(txtAmount.Text);
您只需确保在文本框中只键入数字,例如1 2 3 如果您键入任何非数字字符,如字母等。。那么它就不能转换成等价的整数。例如字符串:
“234”
可以转换为整数,但“23A4”
不能
以下是3种转换为整数的方法:
Int32.Parse()
,Convert.ToInt32()
,和Int32.TryParse()
在这三个函数中,Int32.TryParse()
不会在出错时引发异常。相反,它会在转换过程中遇到任何错误时返回0
Int32.TryParse(string s, out int)
When s is a null reference, it will return 0 rather than throw
ArgumentNullException. If s is other than an integer value, the out
variable will have 0 rather than FormatException. When s represents
a number less than MinValue or greater than MaxValue, the out variable
will have 0 rather than OverflowException
检查以获得对这三种方法的完整理解。简单地说,
txtmount.Text
不是一个数字。txtmount.Text
的值到底是多少?