2017-02-14 52 views
2

我有什麼用,而不是ToInt32,轉換長串號值「10000000001」爲整數,所以這種方式僅限於10位數字:如何長字符串數值轉換爲整數

string str1 = "1000000000"; 
    string str2 = "1000000000"; 

    int a = Convert.ToInt32(str1); 
    int b = Convert.ToInt32(str2); 

    int c = a + b; 

    Console.WriteLine(c); 

結果:

2000000000 

,但如何在字符串數值越大則十位數字轉換:

string str1 = "10000000001"; 
    string str2 = "10000000001"; 

得到結果:

20000000002 

回答

2

如果該值可能是在它可能是大或小的任何數量,你能想到的,然後使用它在System.Numerics命名空間中找到的BigInteger結構的任意數量的。

例如:

string str1 = "1000023432432432432234234324234324432432432432400000"; 
    string str2 = "1003240032432432423432432948320849329493294832800000"; 

    BigInteger BigInt = (BigInteger.Parse(str1) + BigInteger.Parse(str2)); // might want to validate before doing this. 
    Console.WriteLine(BigInt); 

基本上,BigInteger的沒有上限或下限的限制。唯一的限制就是你的RAM。

但是,如果你的號碼將以小數量超過10位,那麼你可以使用int64。長數據類型。

+1

非常有幫助謝謝 – nikorio

相關問題