2014-07-09 172 views
1

如何将字符串“09335887170”转换为整数?这是我曾尝试过的:将字符串“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()); 
+7

这个数字对于一个整数来说太大了。你有没有考虑过使用很长时间? –

+1

签出Long或BigInteger。另外为了获取详细信息,只需使用“解析” - 方法来获得明确的例外。 –

+0

1.使用正常的'Parse' 2.运行代码并得到一个错误3. Google错误信息....然后你会得到你的解决方案,然后问这个问题要容易得多 – musefan

回答

9

Int32(或仅int)的最大值为2,147,483,647 你需要一个UInt64Int64Long

string a = "09335887170"; 
Int64 myInt; 
bool isValid = Int64.TryParse(a, out myInt); 
if (isValid) 
{ 
    int plusOne = myInt + 1; 
    MessageBox.Show(plusOne.ToString()); 
} 
+3

或者只是一个Int64表示的“Long” 。 ;) –

+0

这实际上并没有编译。你应该使用'long'或'Int64'。 – recursive

+1

没有'int64',只是'Int64'或'long'。 – Jodrell

1

该数字超过了最大整数值。用长一点的。

1

试试这个

Int64 no = Convert.ToInt64( "09335887170"); 
+0

使用'Parse' not'Convert' – Jodrell

3
result = Convert.ToInt64(value); 

here更多信息。

(网上找到,我不是真的在C#中)