GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
http://cnsendnet.taobao.com
来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=612php
适用于
.NET Core
3.0 Preview 2 2.2 2.1 2.0 1.1 1.0
.NET Framework
4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6网络
今天遇到一个要处理XSD中Integer的数值区间的计算的问题,Integer这个类型的值区间理论上是可没有边界的,假设目前的值是1.5E+10000, 这个数字已经达到double和Int64都没法存储了,同时我还要对如此大的数字进行加减运算,后来发现了BigInteger这个类能够很好的解决我遇到的问题。^_^
BigInteger
自.net framework 4.0开始引入, 位于命名空间:
namespace System.Numerics
设计用于存储超大整型数字,因此只要内存够大,存储是没有上限和下限的,不然若是数字过大的话,会遇到OutOfMemory的异常。
个人案例
由于个人输入就是一个字符串的数字,因此我调用BigInteger.Parse()方法能够获得一个BigInteger实例,而后就能够对于进行+1 或者 -1的运算了
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(); }
输出结果:
BigInteger还很不少的方法:好比 Min, Max, Substract, Multiply, Divide, Log, Pow, 等等,同时BigInteger对大量的运算符都进行了重载,很方便使用。
更多资料能够参看MSDN System.Numerics.BigIntegeride
GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
http://cnsendnet.taobao.com
来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=612网站