When should I use double instead of decimal?

后端 未结 12 1571
Happy的楠姐
Happy的楠姐 2020-11-22 10:51

I can name three advantages to using double (or float) instead of decimal:

  1. Uses less memory.
  2. Faster because flo
相关标签:
12条回答
  • 2020-11-22 11:31

    Use decimal for base 10 values, e.g. financial calculations, as others have suggested.

    But double is generally more accurate for arbitrary calculated values.

    For example if you want to calculate the weight of each line in a portfolio, use double as the result will more nearly add up to 100%.

    In the following example, doubleResult is closer to 1 than decimalResult:

    // Add one third + one third + one third with decimal
    decimal decimalValue = 1M / 3M;
    decimal decimalResult = decimalValue + decimalValue + decimalValue;
    // Add one third + one third + one third with double
    double doubleValue = 1D / 3D;
    double doubleResult = doubleValue + doubleValue + doubleValue;
    

    So again taking the example of a portfolio:

    • The market value of each line in the portfolio is a monetary value and would probably be best represented as decimal.

    • The weight of each line in the portfolio (= Market Value / SUM(Market Value)) is usually better represented as double.

    0 讨论(0)
  • 2020-11-22 11:31

    Use floating points if you value performance over correctness.

    0 讨论(0)
  • 2020-11-22 11:36

    In some Accounting, consider the possibility of using integral types instead or in conjunction. For example, let say that the rules you operate under require every calculation result carry forward with at least 6 decimal places and the final result will be rounded to the nearest penny.

    A calculation of 1/6th of $100 yields $16.66666666666666..., so the value carried forth in a worksheet will be $16.666667. Both double and decimal should yield that result accurately to 6 decimal places. However, we can avoid any cumulative error by carrying the result forward as an integer 16666667. Each subsequent calculation can be made with the same precision and carried forward similarly. Continuing the example, I calculate Texas sales tax on that amount (16666667 * .0825 = 1375000). Adding the two (it's a short worksheet) 1666667 + 1375000 = 18041667. Moving the decimal point back in gives us 18.041667, or $18.04.

    While this short example wouldn't yield a cumulative error using double or decimal, it's fairly easy to show cases where simply calculating the double or decimal and carrying forward would accumulate significant error. If the rules you operate under require a limited number of decimal places, storing each value as an integer by multiplying by 10^(required # of decimal place), and then dividing by 10^(required # of decimal places) to get the actual value will avoid any cumulative error.

    In situations where fractions of pennies do not occur (for example, a vending machine), there is no reason to use non-integral types at all. Simply think of it as counting pennies, not dollars. I have seen code where every calculation involved only whole pennies, yet use of double led to errors! Integer only math removed the issue. So my unconventional answer is, when possible, forgo both double and decimal.

    0 讨论(0)
  • 2020-11-22 11:40

    Depends on what you need it for.

    Because float and double are binary data types you have some diifculties and errrors in the way in rounds numbers, so for instance double would round 0.1 to 0.100000001490116, double would also round 1 / 3 to 0.33333334326441. Simply put not all real numbers have accurate representation in double types

    Luckily C# also supports the so-called decimal floating-point arithmetic, where numbers are represented via the decimal numeric system rather than the binary system. Thus, the decimal floating point-arithmetic does not lose accuracy when storing and processing floating-point numbers. This makes it immensely suited to calculations where a high level of accuracy is needed.

    0 讨论(0)
  • 2020-11-22 11:41

    A double values will serialize to scientific notation by default if that notation is shorter than the decimal display. (e.g. .00000003 will be 3e-8) Decimal values will never serialize to scientific notation. When serializing for consumption by an external party, this may be a consideration.

    0 讨论(0)
  • 2020-11-22 11:42

    Decimal has wider bytes, double is natively supported by CPU. Decimal is base-10, so a decimal-to-double conversion is happening while a decimal is computed.

    For accounting - decimal
    For finance - double
    For heavy computation - double
    

    Keep in mind .NET CLR only supports Math.Pow(double,double). Decimal is not supported.

    .NET Framework 4

    [SecuritySafeCritical]
    public static extern double Pow(double x, double y);
    
    0 讨论(0)
提交回复
热议问题