How do you deal with numbers larger than UInt64 (C

2019-01-14 17:07发布

In C#, how can one store and calculate with numbers that significantly exceed UInt64's max value (18,446,744,073,709,551,615)?

7条回答
小情绪 Triste *
2楼-- · 2019-01-14 17:23

You can use decimal. It is greater than Int64.

It has 28-29 significant digits.

查看更多
仙女界的扛把子
3楼-- · 2019-01-14 17:24

Can you use the .NET 4.0 beta? If so, you can use BigInteger.

Otherwise, if you're sticking within 28 digits, you can use decimal - but be aware that obviously that's going to perform decimal arithmetic, so you may need to round at various places to compensate.

查看更多
Ridiculous、
4楼-- · 2019-01-14 17:24

What is it that you wish to use these numbers for? If you are doing calculations with really big numbers, do you still need the accuracy down to the last digit? If not, you should consider using floating point values instead. They can be huge, the max value for the double type is 1.79769313486231570E+308, (in case you are not used to scientific notation it means 1.79769313486231570 multiplied by 10000000...0000 - 308 zeros).

That should be large enough for most applications

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-14 17:27

By using a BigInteger class; there's one in the the J# libraries (definitely accessible from C#), another in F# (need to test this one), and there are freestanding implementations such as this one in pure C#.

查看更多
Bombasti
6楼-- · 2019-01-14 17:30

BigInteger represents an arbitrarily large signed integer.

using System.Numerics;

var a = BigInteger.Parse("91389681247993671255432112000000");
var b = new BigInteger(1790322312);
var c = a * b;
查看更多
smile是对你的礼貌
7楼-- · 2019-01-14 17:33

Decimal has greater range.

There is support for bigInteger in .NET 4.0 but that is still not out of beta.

查看更多
登录 后发表回答