Which one should I use and when?

This question already has an answer here:

  • When should I use double instead of decimal? 12 answers

  • 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".


    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 (ie 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.


    System.Single / float - 7 digits
    System.Double / double - 15-16 digits
    System.Decimal / decimal - 28-29 significant 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.

    链接地址: http://www.djcxy.com/p/3636.html

    上一篇: 最多舍入2位小数(仅在必要时)

    下一篇: 我应该使用哪一个和什么时候?