C# 十进制与双精度!- 我应该在什么时候使用哪一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1165761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:47:52  来源:igfitidea点击:

decimal vs double! - Which one should I use and when?

c#doubledecimalprecisioncurrency

提问by Soni Ali

I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision. My question is when should a use a double and when should I use a decimal type? Which type is suitable for money computations? (ie. greater than $100 million)

我一直看到人们在 C# 中使用双打。我知道我在某处读到双打有时会失去精度。我的问题是什么时候应该使用双精度型,什么时候应该使用十进制类型?哪种类型适合货币计算?(即超过 1 亿美元)

采纳答案by David

For money, alwaysdecimal. It's why it was created.

对于金钱,总是十进制。这就是它被创建的原因。

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

如果数字必须正确相加或平衡,请使用小数。这包括人们可能手工完成的任何财务存储或计算、分数或其他数字。

If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits".

如果数字的确切值不重要,请使用 double 来提高速度。这包括图形、物理或其他物理科学计算,其中已经有“有效数字的数量”。

回答by Clement Herreman

For money: decimal. It costs a little more memory, but doesn't have rounding troubles like doublesometimes has.

为了钱:decimal。它需要更多的内存,但不会像double有时那样有舍入问题。

回答by Otto Allmendinger

Definitelyuse integer types for your money computations.
This cannot be emphasized enough since at first glance it might seem that a floating point type is adequate.

绝对使用整数类型进行货币计算。
这一点怎么强调都不为过,因为乍一看似乎浮点类型就足够了。

Here an example in python code:

这是python代码中的一个示例:

>>> amount = float(100.00) # one hundred dollars
>>> print amount
100.0
>>> new_amount = amount + 1
>>> print new_amount
101.0
>>> print new_amount - amount
>>> 1.0

looks pretty normal.

看起来很正常。

Now try this again with 10^20Zimbabwe dollars:

现在用10^20津巴布韦元再试一次:

>>> amount = float(1e20)
>>> print amount
1e+20
>>> new_amount = amount + 1
>>> print new_amount
1e+20
>>> print new_amount-amount
0.0

As you can see, the dollar disappeared.

如您所见,美元消失了。

If you use the integer type, it works fine:

如果您使用整数类型,它工作正常:

>>> amount = int(1e20)
>>> print amount
100000000000000000000
>>> new_amount = amount + 1
>>> print new_amount
100000000000000000001
>>> print new_amount - amount
1

回答by Ian Boyd

Decimal is for exact values. Double is for approximate values.

十进制用于精确值。Double 用于近似值。

USD: ,345.67 USD (Decimal)
CAD: ,617.27 (Decimal)
Exchange Rate: 1.102932 (Double)

回答by Michael Borgwardt

My question is when should a use a double and when should I use a decimal type?

我的问题是什么时候应该使用双精度型,什么时候应该使用十进制类型?

decimalfor when you work with values in the range of 10^(+/-28) and where you have expectations about the behaviour based on base 10 representations - basically money.

decimal因为当您使用 10^(+/-28) 范围内的值以及您对基于 10 表示的行为有期望时 - 基本上是金钱。

doublefor when you need relativeaccuracy (i.e. losing precision in the trailing digits on large values is not a problem) across wildly different magnitudes - doublecovers more than 10^(+/-300). Scientific calculations are the best example here.

double当您需要相对精度时(即在大值的尾随数字中丢失精度不是问题)跨越大不相同的幅度 -double覆盖超过 10^(+/-300)。科学计算是这里最好的例子。

which type is suitable for money computations?

哪种类型适合货币计算?

decimal, decimal, decimal

十进制,十进制十进制

Accept no substitutes.

不接受替代品。

The most important factor is that double, being implemented as a binary fraction, cannot accurately represent many decimalfractions (like 0.1) at alland its overall number of digits is smaller since it is 64-bit wide vs. 128-bit for decimal. Finally, financial applications often have to follow specific rounding modes(sometimes mandated by law). decimalsupports these; doubledoes not.

最重要的因素是,double作为二进制分数实现,根本无法准确表示许多decimal分数(如 0.1)并且其总位数较少,因为 64 位宽与 128 位宽decimal。最后,金融应用程序通常必须遵循特定的舍入模式(有时是法律规定的)。decimal支持这些double才不是。

回答by Chris S

System.Single/ float- 7 digits
System.Double/ double- 15-16 digits
System.Decimal/ decimal- 28-29 significant digits

System.Single/ float- 7 位
System.Double/ double- 15-16 位
System.Decimal/十进制- 28-29 位有效数字

The way I've been stung by using the wrong type (a good few years ago) is with large amounts:

我因使用错误类型(几年前)而受到伤害的方式是大量使用:

  • £520,532.52 - 8 digits
  • £1,323,523.12 - 9 digits
  • £520,532.52 - 8 位数字
  • £1,323,523.12 - 9 位数字

You run out at 1 million for a float.

你花光了 100 万的浮动。

A 15 digit monetary value:

15 位货币价值:

  • £1,234,567,890,123.45
  • £1,234,567,890,123.45

9 trillion with a double. But with division and comparisons it's more complicated (I'm definitely no expert in floating point and irrational numbers - see Marc's point). Mixing decimals and doubles causes issues:

9 万亿加倍。但是除法和比较会更复杂(我绝对不是浮点数和无理数方面的专家 -请参阅 Marc 的观点)。混合小数和双精度会导致问题:

A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used because the floating-point number might not exactly approximate the decimal number.

如果使用十进制数,则使用浮点数的数学或比较运算可能不会产生相同的结果,因为浮点数可能不完全接近十进制数。

When should I use double instead of decimal?has some similar and more in depth answers.

我什么时候应该使用双精度而不是十进制?有一些类似的和更深入的答案。

Using doubleinstead of decimalfor monetary applicationsis a micro-optimization - that's the simplest way I look at it.

使用double而不是decimal用于货币应用程序是一种微观优化 - 这是我看待它的最简单的方式。

回答by Honzajscz

I think that the main difference beside bit width is that decimal has exponent base 10 and double has 2

我认为除了位宽之外的主要区别是小数的指数基数为 10,而双数的基数为 2

http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html

http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html