十进制与双精度! -应该使用哪一个?何时使用? [重复]

本文翻译自:decimal vs double! - Which one should I use and when? [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

I keep seeing people using doubles in C#. 我一直看到人们在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) (即超过1亿美元)


#1楼

参考:https://stackoom.com/question/4tGb/十进制与双精度-应该使用哪一个-何时使用-重复


#2楼

For money: decimal . 金钱: decimal It costs a little more memory, but doesn't have rounding troubles like double sometimes has. 它的费用多一点的内存,但没有四舍五入的烦恼像double有时也有。


#3楼

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. 如果数字的确切值不重要,请使用double作为速度。 This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits". 这包括图形,物理学或其他物理科学计算,其中已经有“有效位数”。


#4楼

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

#5楼

Decimal is for exact values. 小数表示精确值。 Double is for approximate values. Double是近似值。

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

#6楼

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. 当您使用10 ^(+/- 28)范围内的值以及对基于基数10表示形式的行为有期望的情况时,基本为decimal

double for when you need relative accuracy (ie losing precision in the trailing digits on large values is not a problem) across wildly different magnitudes - double covers more than 10^(+/-300). double当你需要相对精确度(即在大的值的结尾数字失去精度不是问题)跨越很大的不同幅度- double覆盖超过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 . 最重要的因素是,以二进制分数形式实现的double根本无法准确表示许多decimal分数(例如0.1) 并且其总位数较小,因为它的宽度为64位,而decimal为128位。 Finally, financial applications often have to follow specific rounding modes (sometimes mandated by law). 最后,财务申请通常必须遵循特定的舍入模式 (有时是法律规定的)。 decimal supports these ; decimal 支持这些 ; double does not. double没有。

发布了0 篇原创文章 · 获赞 73 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105363431