如何将像“09335887170”这样的字符串转换为整数?这是我尝试过的:
string a = "09335887170";
int myInt;
bool isValid = int.TryParse(a, out myInt); // it returns false in this case
if (isValid)
{
int plusOne = myInt + 1;
MessageBox.Show(plusOne.ToString());
}
MessageBox.Show(a);
int stringToInt = Convert.ToInt32("09335887170"); // it returns nothing with no error
MessageBox.Show((stringToInt + 1).ToString());
int test = int.Parse(a); //it has type convertion error
MessageBox.Show((test + 1).ToString());
答案 0 :(得分:9)
Int32
(或仅int
)的最大值为2,147,483,647
您需要UInt64
或Int64
或Long
string a = "09335887170";
Int64 myInt;
bool isValid = Int64.TryParse(a, out myInt);
if (isValid)
{
int plusOne = myInt + 1;
MessageBox.Show(plusOne.ToString());
}
答案 1 :(得分:3)
答案 2 :(得分:1)
该数字超过max int值。使用长而不是。
答案 3 :(得分:1)
试试这个
Int64 no = Convert.ToInt64( "09335887170");