Today, we have a problem with the calculation of the numeric range of integers in XSD, where the value interval of integer is theoretically borderless, assuming that it is currently 1.5E+10000 of the value and that the number has reached double and Int64 cannot be stored. At the same time I have to add and subtract such a large number of numbers, and later found that BigInteger this class can be a good solution to the problems I encountered. ^_^
BigInteger
Introduced from the. NET Framework 4.0, located in the namespace:
namespace System.Numerics
Designed to store oversized integer numbers, so as long as the memory is large enough, there is no upper and lower limit of storage, otherwise if the number is too large, you will encounter OutOfMemory exception.
My case
Because my input is a string number, so I call the Biginteger.parse () method to get a BigInteger instance, and then we can do +1 or 1 of the operation
static void Main(string[] args) { String largeNum = "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; var number = BigInteger.Parse(largeNum); var numberDecreaseOne = number - 1; var numberIncreaseOne = number + 1; Console.WriteLine(numberDecreaseOne); Console.WriteLine(" "); Console.WriteLine(numberIncreaseOne); Console.ReadKey(); }
Output Result:
BigInteger also a lot of methods: such as Min, Max, Substract, Multiply, Divide, Log, Pow, and so on, while BigInteger to a large number of operators are overloaded, very convenient to use.
For more information, see MSDN System.Numerics.BigInteger
C # BigInteger handles super-large integer numbers