Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
double a = 0.1 + 0.2;
decimal b = 0.1m + 0.2m;
Console.WriteLine(a == 0.3);
Console.WriteLine(b == 0.3m);False True
- Expecting
0.1 + 0.2 == 0.3to betrue:doublestores binary fractions, so the sum is0.30000000000000004. - Expecting
decimalto miss as well: it stores decimal digits, so0.1m + 0.2mis exactly0.3m.
- How would you check that two
doublevalues are equal within a tolerance? - Why is
decimalpreferred for money even though it is slower thandouble?