0

Consider the code...

double first = 19.99;
double second = 19;
double firstPlusSecond = first + second;
Console.WriteLine("Result: " + firstPlusSecond);

The output of this is "Result: 38.99" - which is what I expect

However...inspecting firstPlusSecond in debug, the number is actually 38.989999999999995

Which is messing with calculations elsewhere in the program.

What...? What have I missed?

Paul
  • 9,409
  • 13
  • 64
  • 113
  • This resolves the issue - but what's up with the double? – Paul Aug 25 '17 at 10:14
  • Nope, inspecting 19.99 - it's 19.99... – Paul Aug 25 '17 at 10:15
  • Rounding doubles can be painful indeed. Check out this link for a bit in-depth details: http://www.exploringbinary.com/double-rounding-errors-in-floating-point-conversions/ (not C#, but the same still goes) – Jan_V Aug 25 '17 at 10:18

0 Answers0