0

code:

Console.WriteLine(8.3-8);
Console.WriteLine(8.2-8);
Console.WriteLine(7.2-7);

the code above will output:

0.300000000000001
0.199999999999999
0.2
smallerpig
  • 27
  • 5
  • 1
    [Representation of floating point numbers](https://msdn.microsoft.com/en-us/library/c151dt3s.aspx). Try `8.3m-8` instead – CodingIntrigue Jun 11 '15 at 12:19
  • [This](http://stackoverflow.com/questions/9890477/c-sharp-wrong-subtraction-12-345-12-0-345000000000001) is rather a duplicate and [here](http://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net/618596#618596) you find a good explanation. – Tim Schmelter Jun 11 '15 at 12:20

1 Answers1

5

that's the double rounding issue. SO is full of it. Try to work with Decimal

Console.WriteLine((decimal)8.3 - 8);
Console.WriteLine((decimal)8.2 - 8);
Console.WriteLine((decimal)7.2 - 7);
fubo
  • 44,811
  • 17
  • 103
  • 137
  • SO should implement an AI to auto answer such IndexoutOfRange, Nullreference and Double rounding stuff :D – fubo Jun 11 '15 at 12:20
  • 2
    Instead of `(decimal)8.3` you can use `8.3m` for shorter notation. – S_F Jun 11 '15 at 12:21