0

I have tested this code :

Console.WriteLine(Math.Round(10.4, 0));
Console.WriteLine(Math.Round(10.5, 0));
Console.WriteLine(Math.Round(10.6, 0));
Console.WriteLine(Math.Round(10.04, 1));
Console.WriteLine(Math.Round(10.05, 1));
Console.WriteLine(Math.Round(10.06, 1));

Console.WriteLine(Math.Round(10.004, 2));
Console.WriteLine(Math.Round(10.005, 2));
Console.WriteLine(Math.Round(10.006, 2));

Console.WriteLine(Math.Round(10.0004, 3));
Console.WriteLine(Math.Round(10.0005, 3));
Console.WriteLine(Math.Round(10.0006, 3));

for Math.Round(10.005, 2) the result is 10.01 while for Math.Round(10.5, 0), Math.Round(10.05, 1) or Math.Round(10.0005,3) the result is 10.

Why Math.Round(10.005, 2) return 10.01 and not 10 ? If default MidpointRounding is Even, why is not the case when i have 2 digits ?

What is the reason of this comportement ?

tmryuga
  • 1
  • 1
  • Define: comportement – rory.ap Apr 13 '16 at 14:11
  • 2
    In general, it could be one of two reasons: 1. [`Math.Round`](https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx) defaults to rounding to the nearest _even_ number (or decimal digit), 2) `double` has some imprecision, so `X.5` may not be _exactly_ `X.5`, it may be `X.49999999999998` – D Stanley Apr 13 '16 at 14:13

0 Answers0