十进制与双精度! - 我应该使用哪一种以及何时使用?
我不断看到人们在 C# 中使用双精度。 我知道我在某处读到双倍有时会失去精度。 我的问题是什么时候应该使用双精度类型,什么时候应该使用十进制类型? 哪种类型适合货币计算? (即超过 1 亿美元)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于金钱,总是十进制。 这就是它被创建的原因。
如果数字必须正确相加或平衡,请使用小数。 这包括任何财务存储或计算、分数或人们可能手工完成的其他数字。
如果数字的确切值并不重要,请使用 double 来提高速度。 这包括图形、物理或其他物理科学计算,其中已经存在“有效数字位数”。
For money, always decimal. 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".
decimal
用于当您使用 10^(+/-28) 范围内的值并且您对基于 10 表示的行为有期望时 - 基本上是金钱。double
用于当您需要在截然不同的量级上相对精度(即在大值的尾随数字中丢失精度不是问题)时 -double
覆盖超过 10^(+/-300)。 科学计算就是最好的例子。小数、小数、小数
不接受替代品。
最重要的因素是,
double
作为二进制分数实现,无法准确表示许多十进制
分数(例如 0.1)根本及其总位数较少,因为它是 64 位宽,而十进制
是 128 位宽。 最后,金融应用程序通常必须遵循特定的舍入模式(有时是法律强制要求的)。十进制
支持这些;double
没有。decimal
for 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.double
for when you need relative accuracy (i.e. losing precision in the trailing digits on large values is not a problem) across wildly different magnitudes -double
covers more than 10^(+/-300). Scientific calculations are the best example here.decimal, decimal, decimal
Accept no substitutes.
The most important factor is that
double
, being implemented as a binary fraction, cannot accurately represent manydecimal
fractions (like 0.1) at all and its overall number of digits is smaller since it is 64-bit wide vs. 128-bit fordecimal
. Finally, financial applications often have to follow specific rounding modes (sometimes mandated by law).decimal
supports these;double
does not.根据 浮点类型的特征:
System.Single
System.Double
System.Decimal
我因使用错误的类型而被刺痛(好几个年前)的金额很大:
您用完了 100 万的浮点数。
15 位数字的货币价值:
9 万亿,双倍。 但通过除法和比较,情况会更加复杂(我绝对不是浮点和无理数方面的专家 - 参见马克的观点)。 混合小数和双精度会导致问题:
什么时候应该使用双精度而不是十进制? 有一些类似且更深入的答案。
对于货币应用程序,使用
double
而不是decimal
是一种微观优化 - 这是我看待它的最简单的方式。According to Characteristics of the floating-point types:
System.Single
System.Double
System.Decimal
The way I've been stung by using the wrong type (a good few years ago) is with large amounts:
You run out at 1 million for a float.
A 15 digit monetary value:
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:
When should I use double instead of decimal? has some similar and more in depth answers.
Using
double
instead ofdecimal
for monetary applications is a micro-optimization - that's the simplest way I look at it.小数表示精确值。 Double 用于近似值。
Decimal is for exact values. Double is for approximate values.
对于金钱:
十进制
。 它会花费更多的内存,但不会像double
有时那样出现舍入问题。For money:
decimal
. It costs a little more memory, but doesn't have rounding troubles likedouble
sometimes has.绝对使用整数类型进行金钱计算。
这一点怎么强调都不为过,因为乍一看似乎浮点类型就足够了。
这里有一个 python 代码的例子:
看起来很正常。
现在用
10^20
津巴布韦元再次尝试:如您所见,美元消失了。
如果你使用整数类型,它工作得很好:
Definitely use 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:
looks pretty normal.
Now try this again with
10^20
Zimbabwe dollars:As you can see, the dollar disappeared.
If you use the integer type, it works fine:
我认为除了位宽之外的主要区别是,decimal 的指数基数为 10,而 double 的指数基数为 2
http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html
I think that the main difference beside bit width is that decimal has exponent base 10 and double has 2
http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html