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.
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):