2021年2月2日星期二

C# if statement and decimal variable is less than 1 and greater than 0

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)?

https://stackoverflow.com/questions/66021092/c-sharp-if-statement-and-decimal-variable-is-less-than-1-and-greater-than-0 February 03, 2021 at 12:02PM

没有评论:

发表评论