十进制与双精度! - 我应该使用哪一种以及何时使用?

发布于 2024-07-27 21:26:14 字数 109 浏览 2 评论 0原文

我不断看到人们在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蹲墙角沉默 2024-08-03 21:26:14

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

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

如果数字的确切值并不重要,请使用 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".

笨死的猪 2024-08-03 21:26:14

我的问题是什么时候应该使用
double 以及什么时候应该使用小数
类型?

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

double 用于当您需要在截然不同的量级上相对精度(即在大值的尾随数字中丢失精度不是问题)时 - double覆盖超过 10^(+/-300)。 科学计算就是最好的例子。

哪种类型适合钱
计算?

小数、小数小数

不接受替代品。

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

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

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.

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 decimal fractions (like 0.1) at all and 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). decimal supports these; double does not.

做个少女永远怀春 2024-08-03 21:26:14

根据 浮点类型的特征

.NET 类型C# 关键字精度
System.Singlefloat~6-9 位
System.Doubledouble~15-17 位
System.Decimal十进制28-29 位

我因使用错误的类型而被刺痛(好几个年前)的金额很大:

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

您用完了 100 万的浮点数。

15 位数字的货币价值:

  • £1,234,567,890,123.45

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

数学或比较运算
使用浮点数
可能不会产生相同的结果,如果
使用十进制数是因为
浮点数可能不会
精确近似小数
数量。

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

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

According to Characteristics of the floating-point types:

.NET TypeC# KeywordPrecision
System.Singlefloat~6-9 digits
System.Doubledouble~15-17 digits
System.Decimaldecimal28-29 digits

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

You run out at 1 million for a float.

A 15 digit monetary value:

  • £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:

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 double instead of decimal for monetary applications is a micro-optimization - that's the simplest way I look at it.

拒绝两难 2024-08-03 21:26:14

小数表示精确值。 Double 用于近似值。

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

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

USD: $12,345.67 USD (Decimal)
CAD: $13,617.27 (Decimal)
Exchange Rate: 1.102932 (Double)
日久见人心 2024-08-03 21:26:14

对于金钱:十进制。 它会花费更多的内存,但不会像 double 有时那样出现舍入问题。

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

記柔刀 2024-08-03 21:26:14

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

这里有一个 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

看起来很正常。

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

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

如您所见,美元消失了。

如果你使用整数类型,它工作得很好:

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

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:

>>> 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^20 Zimbabwe dollars:

>>> 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
遇见了你 2024-08-03 21:26:14

我认为除了位宽之外的主要区别是,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文