C # BigInteger handles super-large integer numbers

Source: Internet
Author: User
Tags integer numbers

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.