Why does System.MidpointRounding.AwayFromZero not round up in this instance?

Your suspicion is exactly right. Numbers with fractional portion, when expressed as literals in .NET, are by default doubles. A double (like a float) is an approximation of a decimal value, not a precise decimal value. It is the closest value that can be expressed in base-2 (binary). In this case, the approximation is ever so vanishingly on the small side of 1.035. If you write it using an explicit Decimal it works as you expect:

Console.WriteLine(Math.Round(1.035m, 2, MidpointRounding.AwayFromZero));
Console.ReadKey();

To understand why doubles and floats work the way they do, imagine representing the number 1/3 in decimal (or binary, which suffers from the same problem). You can’t- it translates to .3333333…., meaning that to represent it accurately would require an infinite amount of memory.

Computers get around this using approximations. I’d explain precisely how, but I’d probably get it wrong. You can read all about it here though: http://en.wikipedia.org/wiki/IEEE_754-1985

Leave a Comment