I am using Visual Studio Professional 2012. I created a new C# ConsoleApplication, targeting .NET Framework 4.5, with following code:
static void Main(string[] args)
{
double x = 2.44445;
double y = Math.Round(x, 4, MidpointRounding.AwayFromZero);
Console.WriteLine(y);
Console.ReadKey();
}
The expected result should be 2.4445, but it actually returns 2.4444. //Same result with previous framework version, and I tried VCE2010.
I know such problem usually results from the way double data type is stored (i.e. finite decimals converted to infinite binary fraction). But I didn't expect this to happen with only 5 decimal digits like 2.44445
I'm worrying if such thing could happen with even shorter decimals. I would also like to learn a safer way to round (using away from zero convention) in C#. Thanks.