BigInteger Data Type in C#


In C#, all numeric data types store limited range of values. For example, Int32 data type can store integers from -2,147,483,648 to 2,147,483,647. The long (Int64) type can store integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, and unsigned long has the limit of 0 to 18,446,744,073,709,551,615.

To remove these limitations, C# includes the BigInteger data type that represents an arbitrarily large signed integer that has no upper or lower limits.

BigInteger is an immutable struct type included in the System.Numerics namespace.

Use of BigInteger

Create the object of the BigInteger by assigning or passing different types of values such as byte[], Decimal, Double, Int32, Long, UInt32, and UInt64, in the constructors, as shown below:

Example: Create BigInteger Objects
BigInteger bi1 = 2147483647;
Console.WriteLine("BigInteger: {0}", bi1);

BigInteger bi2 = new BigInteger(2147483647);
Console.WriteLine("BigInteger from double value: {0}", bi2);
																
BigInteger bi3 = new BigInteger(9223372036854775807);
Console.WriteLine("BigInteger from Int64 value: {0}", bi3);
																
BigInteger bi4 = new BigInteger(18446744073709551615);
Console.WriteLine("BigInteger from UInt64 value: {0}", bi4);
Output:
BigInteger: 2147483647
BigInteger from double value: 2147483647
BigInteger from Int64 value: 9223372036854775807
BigInteger from UInt64 value: 18446744073709551615

However, to assign larger values than UInt64, you will have to parse the numeric string to BigInteger using the BigInteger.Parse() or BigInteger.TryParse() methods, as shown below.

Example: Large Value of BigInteger
string strPositive = "24158320451772550623816883111000";
string strNegative = "-61224366321537228109113522100938100";
string str = "3424324234244234234abc";
		
BigInteger biPositive = 0, biNegative = 0, biNAN;
		
biPositive = BigInteger.Parse(strPositive);
Console.WriteLine("Positive BigInteger: {0}", biPositive);
		
if (BigInteger.TryParse(strNegative, out biNegative))
	Console.WriteLine("Negative BigInteger: {0}", biNegative);
else
	Console.WriteLine("Cannot convert from {0} to BigInteger", strNegative);
		
if (BigInteger.TryParse(str, out biNAN))
	Console.WriteLine("BigInteger: {0}", biNAN);
else
	Console.WriteLine("Cannot convert from {0} to BigInteger", str);
Output:
Positive BigInteger: 24158320451772550623816883111000
Negative BigInteger: -61224366321537228109113522100938100

Use the ToString() method to convert from BigInteger to string in different format, and the Parse() method to hex or other numeric strings to BigInteger, as shown below.

Example:
BigInteger bi = 57333089;
		
string hex = bi.ToString("X");
Console.WriteLine("BigInteger {0} to Hexadecimal Value: {1}", bi, hex);
		
//other formats
Console.WriteLine(bi.ToString("F"));
Console.WriteLine(bi.ToString("N"));

var bi2 = BigInteger.Parse(hex,  System.Globalization.NumberStyles.HexNumber);
Console.WriteLine("Converted from Hex {0} to BigInteger: {1}", hex, bi2);
Output:
BigInteger 57333089 to Hexadecimal Value: 36AD561
57333089.00
57,333,089.00
Converted from Hex 36AD561 to BigInteger: 57333089