20

I need to compare two values. One value is price which represents current price of something and so decimal because you actually can buy or sell something by this price.

Another value is estimatedPrice which is the result of mathematical calculations and so double because it's just estimation and you can not actually do any "operations" by this esitmatedPrice

now i want to buy if price < estimatedPrice.

So should i cast price to double or estimatedPrice to decimal?

I understand that after casting I will get "slighty another" numbers, but it seems I don't have other options.

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • @BlueChippy you can't use float for price data if the items are expensive. For example, float cannot represent `123456.78`. – phoog May 16 '12 at 05:13
  • Decimal takes up more memory and has more significant numbers with a smaller range and therefore can be "more precise" (depending on your view of "precise") compared to floating points (float/double). You could quite happily use double for both values, or decimal, or float if you want, especially if Price is always going to be to two decimal places. You cannot explicitly convert from any floating point to decimal (as they are completely different things), so might be best in this case to choose one for all types. Will leave it to others to recommend which one! (I'd go decimal, personally) – BlueChippy May 16 '12 at 05:16
  • @phoog True, depends on the scenario. My personal pref is either decimal (preferred) or double...but use that type across the board if possible. – BlueChippy May 16 '12 at 05:18
  • @BlueChippy agreed. I actually made the comment about float because I've even seen cases where money values exceeded the *double* range. However, I haven't seen that since Italy went to the euro and Turkey revalued its currency by a factor of 10^6. – phoog May 16 '12 at 05:22

4 Answers4

18

It depends on the data. Decimal has greater precision; double has greater range. If the double could be outside the decimal range, you should cast the decimal to double (or indeed you could write a method that returns a result without casting, if the double value is outside the decimal range; see example below).

In this case, it seems unlikely that the double value would be outside the decimal range, so (especially since you're working with price data) you should cast the double to decimal.

Example (could use some logic to handle NaN values):

private static int Compare(double d, decimal m)
{
    const double decimalMin = (double)decimal.MinValue;
    const double decimalMax = (double)decimal.MaxValue;
    if (d < decimalMin) return -1;
    if (d > decimalMax) return 1;
    return ((decimal)d).CompareTo(m);
}
phoog
  • 42,068
  • 6
  • 79
  • 117
4

decimal vs double! - Which one should I use and when?

If you're more concerned with precision, convert to decimal. If you're not, go with doubles.

Community
  • 1
  • 1
Yatrix
  • 13,361
  • 16
  • 48
  • 78
0

I've never worked with prices before in software, but from what I've heard, many deal with integers. For example, for $1.23, store it as the integer 123. The conversion to a decimal is done at the very end when you output results to the user.

Similarly, for your estimated price, can you deal with numbers that are (say) 100 times larger?

Ray
  • 880
  • 1
  • 10
  • 18
  • 1
    this could be a good optimization, however right now I should either cast double to decimal or decimal to double. Also I guess probably `decimal` inside use `int` so it would be easier to just use `decimal` for price. – Oleg Vazhnev May 16 '12 at 05:12
0

I recommend you to convert to decimal. because it seems you want manipulate money values. but I can give this short answer : for accurate (manipulating money value specially for financial applications) application use decimal. and if you prefer less resource and more speed use double.

Shahin
  • 302
  • 2
  • 10