你如何处理大于 UInt64 (C#) 的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1298787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you deal with numbers larger than UInt64 (C#)
提问by Alex
In C#, how can one store and calculate with numbers that significantly exceed UInt64's max value (18,446,744,073,709,551,615)?
在 C# 中,如何存储和计算显着超过 UInt64 的最大值 (18,446,744,073,709,551,615) 的数字?
采纳答案by Steve Gilham
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 onein pure C#.
通过使用 BigInteger 类;J# 库中有一个(绝对可以从 C# 访问),另一个在 F# 中(需要测试这个),还有一些独立的实现,比如纯 C# 中的这个。
回答by Josip Medved
Decimal has greater range.
十进制有更大的范围。
There is support for bigInteger in .NET 4.0 but that is still not out of beta.
.NET 4.0 中支持 bigInteger,但它仍然没有超出测试版。
回答by Jon Skeet
Can you use the .NET 4.0 beta? If so, you can use BigInteger
.
您可以使用 .NET 4.0 测试版吗?如果是这样,您可以使用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.
否则,如果您坚持在 28 位以内,则可以使用decimal
- 但请注意,这显然将执行十进制算术,因此您可能需要在各个位置进行四舍五入以进行补偿。
回答by Filip Navara
回答by Wael Dalloul
回答by Pete
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).
你想用这些数字做什么?如果您正在使用非常大的数字进行计算,您还需要精确到最后一位吗?如果没有,您应该考虑改用浮点值。它们可能很大,double 类型的最大值为 1.79769313486231570E+308,(如果您不习惯科学记数法,则表示 1.79769313486231570 乘以 10000000...0000 - 308 个零)。
That should be large enough for most applications
对于大多数应用程序来说应该足够大
回答by pomber
BigIntegerrepresents an arbitrarily large signed integer.
BigInteger表示任意大的有符号整数。
using System.Numerics;
var a = BigInteger.Parse("91389681247993671255432112000000");
var b = new BigInteger(1790322312);
var c = a * b;
回答by tomRedox
Also, do check that you truly need a variable with greater capacity than Int64 and aren't falling foul of C#'s integer arithmetic.
另外,请检查您是否确实需要一个容量比 Int64 更大的变量,并且没有违反 C# 的整数算法。
For example, this code will yield an overflow error:
例如,此代码将产生溢出错误:
Int64 myAnswer = 20000*1024*1024;
At first glance that might seem to be because the result is too large for an Int64
variable, but actually it's because each of the numbers on the right side of the formula are implicitly typed as Int32
so the temporary memory space reserved for the result of the calculation will be Int32
size, and that's what causes the overflow.
乍一看似乎是因为结果对于Int64
变量来说太大了,但实际上是因为公式右侧的每个数字都是隐式键入的,Int32
因此为计算结果保留的临时内存空间将是Int32
大小,这就是导致溢出的原因。
The result will actually easily fit into an Int64
, it just needs one of the values on the right to be cast to Int64
:
结果实际上很容易适合Int64
,它只需要将右侧的值之一强制转换为Int64
:
Int64 myAnswer = (Int64)20000*1024*1024;
This is discussed in more detail in this answer.
(I appreciate this doesn't apply in the OP's case, but it was just this sort of issue that brought me here!)
(我很欣赏这不适用于 OP 的情况,但正是这种问题将我带到了这里!)