-3

I'm new to programing and figured out that if you are dealing with money, decimal is better to use. I noticed that if I use a if statement like,

if (salePrice < 0.01)

If salePrice is a decimal, it will not work because .01 represents a double-precision floating-point number. If I change everything to double it does work. But, I need to keep everything as a decimal and you really can't mix decimal with double as far as I know. Setting it to if (salePrice == 0) will still show $0.00 by taking the itemCost - (itemCost * 99.99...%) is not what I want. Is there any way around this or am I stuck with if (salePrice < 1)?

  • Does this answer your question? [Cast a Double Variable to Decimal](https://stackoverflow.com/questions/6007159/cast-a-double-variable-to-decimal) – Andrew Shepherd Feb 03 '21 at 04:09

1 Answers1

3

Use the 'm' postfix to specify it's a decimal, not a float or double:

//This works
if (salePrice < 0.01m)

See the documentation for more details.

Zer0
  • 7,191
  • 1
  • 20
  • 34