简体   繁体   English

C#十进制和双精度

[英]C# decimal and double

From what I understand decimal is used for precision and is recommended for monetary calculations. 据我了解,十进制用于精度,建议用于货币计算。 Double gives better range, but less precision and is a lot faster than decimal. 双精度提供更好的范围,但精度较低,并且比十进制快很多。

What if I have time and rate, I feel like double is suited for time and decimal for rate. 如果我有时间和汇率,该怎么办?我觉得double适合时间,而十进制适合汇率。 I can't mix the two and run calculations without casting which is yet another performance bottleneck. 我不能将两者混在一起并在不进行强制转换的情况下运行计算,这是另一个性能瓶颈。 What's the best approach here? 最好的方法是什么? Just use decimal for time and rate? 只是使用小数表示时间和费率?

Use double for both. 两者都使用double decimal is for currency or other situations where the base-10 representation of the number is important. decimal适用于货币或其他以10为底的数字表示形式很重要的情况。 If you don't care about the base-10 representation of a number, don't use decimal . 如果您不关心数字以10为底的表示形式,请不要使用decimal For things like time or rates of change of physical quantities, the base-10 representation generally doesn't matter, so decimal is not the most appropriate choice. 对于时间或物理量变化率之类的东西,以10为基数的表示形式通常并不重要,因此decimal不是最合适的选择。

The important thing to realize is that decimal is still a floating-point type. 要意识到的重要一点是, decimal仍然是浮点类型。 It still suffers from rounding error and cannot represent certain "simple" numbers (such as 1/3). 它仍然遭受舍入误差并且不能表示某些“简单”数字(例如1/3)。 Its one advantage (and one purpose) is that it can represent decimal numbers with fewer than 29 significant digits exactly. 它的一个优点(也是一种目的)是,它可以精确地表示少于29个有效数字的十进制数。 That means numbers like 0.1 or 12345.6789. 这意味着像0.1或12345.6789这样的数字。 Basically any decimal you can write down on paper with fewer than 29 digits. 基本上,您可以在纸上写下少于29位数字的任何小数。 If you have a repeating decimal or an irrational number, decimal offers no major benefits. 如果您有重复的小数或无理数,则decimal没有太大的好处。

The rule of thumb is to use the type that is more suitable to the values you will handle. 经验法则是使用更适合您将要处理的值的类型。 This means that you should use DateTime or TimeSpan for time, unless you only care about a specific unit, like seconds, days, etc., in which case you can use any integer type. 这意味着您应该使用DateTimeTimeSpan作为时间,除非您只关心特定的单位,例如秒,天等,在这种情况下,您可以使用任何整数类型。 Usually for time you need precision and don't want any error due to rounding, so I wouldn't use any floating point type like float or double . 通常情况下,您需要精度并且不希望由于舍入而导致任何错误,因此我不会使用任何浮点类型,例如floatdouble

For anything related to money, of course you don't want any rounding error either, so you should really use decimal here. 对于任何与金钱相关的东西,您当然也不需要任何舍入误差,因此您实际上应该在此处使用decimal

Finally, only if for some very specific requirements you need absolute speed in a calculation that is done millions of times and for which decimal happens not to be fast enough, only then I would think of using another faster type. 最后,只有对于某些非常特定的要求,您需要在进行 数百万次计算的过程中需要绝对速度并且decimal恰好不够快时, 可以考虑使用另一种更快的类型。 I would first try with integer values (maybe multiplying your value by a power of 10 if you have decimals) and only divide by this power of 10 at the end. 我将首先尝试使用整数值(如果有小数,则可以将值乘以10的幂),最后只除以10的幂。 If this can't be done, only then I would think of using a double . 如果无法做到这一点, 那么我只会考虑使用double Don't do a premature optimization if you are not sure it's needed. 如果不确定是否需要过早的优化 ,请不要进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM