I have code:
Math.Round(45.4545m, 2, MidpointRounding.AwayFromZero)
It result is 45.45. Looks like it should be 45.46 with MidpointRounding.AwayFromZero. What I am doing wrong?
45.46 instead of 45.45 with MidpointRounding.AwayFromZero
>Solution :
From MidpointRounding documentation:
The strategy of rounding to the nearest number, and when a number is halfway between two others, it’s rounded toward the nearest number that’s away from zero.
Rounding 45.4545 to 2 decimals means you’re taking 45.45 and deciding how to round the .004. As General Grievance says in their comment, that’s not halfway between two others, it’s closer to .00 than 0.01, so it will round to 45.45, not 45.46.
The following code:
Console.WriteLine($"{Math.Round(45.451m, 2, MidpointRounding.AwayFromZero)} = Math.Round({45.451m}, 2, MidpointRounding.AwayFromZero)");
Console.WriteLine($"{Math.Round(45.452m, 2, MidpointRounding.AwayFromZero)} = Math.Round({45.452m}, 2, MidpointRounding.AwayFromZero)");
Console.WriteLine($"{Math.Round(45.453m, 2, MidpointRounding.AwayFromZero)} = Math.Round({45.453m}, 2, MidpointRounding.AwayFromZero)");
Console.WriteLine($"{Math.Round(45.454m, 2, MidpointRounding.AwayFromZero)} = Math.Round({45.454m}, 2, MidpointRounding.AwayFromZero)");
Console.WriteLine($"{Math.Round(45.455m, 2, MidpointRounding.AwayFromZero)} = Math.Round({45.455m}, 2, MidpointRounding.AwayFromZero)");
Console.WriteLine($"{Math.Round(45.456m, 2, MidpointRounding.AwayFromZero)} = Math.Round({45.456m}, 2, MidpointRounding.AwayFromZero)");
gives the output:
45.45 = Math.Round(45.451, 2, MidpointRounding.AwayFromZero)
45.45 = Math.Round(45.452, 2, MidpointRounding.AwayFromZero)
45.45 = Math.Round(45.453, 2, MidpointRounding.AwayFromZero)
45.45 = Math.Round(45.454, 2, MidpointRounding.AwayFromZero)
45.46 = Math.Round(45.455, 2, MidpointRounding.AwayFromZero)
45.46 = Math.Round(45.456, 2, MidpointRounding.AwayFromZero)
You may be thinking of the Math.Floor method, see and example of its use in the question Rounding down to 2 decimal places in c#
For the example given in your question:
Console.WriteLine($"{Math.Floor((45.4545) * 100) / 100} = Math.Floor((45.4545) * 100) / 100");
Console.WriteLine($"{Math.Floor((45.4541) * 100) / 100} = Math.Floor((45.4541) * 100) / 100");
Console.WriteLine($"{Math.Floor((45.4542) * 100) / 100} = Math.Floor((45.4542) * 100) / 100");
Console.WriteLine($"{Math.Floor((45.4543) * 100) / 100} = Math.Floor((45.4543) * 100) / 100");
Console.WriteLine($"{Math.Floor((45.4544) * 100) / 100} = Math.Floor((45.4544) * 100) / 100");
Console.WriteLine($"{Math.Floor((45.4545) * 100) / 100} = Math.Floor((45.4545) * 100) / 100");
Console.WriteLine($"{Math.Floor((45.4546) * 100) / 100} = Math.Floor((45.4546) * 100) / 100");
gives the output:
45.45 = Math.Floor((45.4545) * 100) / 100
45.45 = Math.Floor((45.4541) * 100) / 100
45.45 = Math.Floor((45.4542) * 100) / 100
45.45 = Math.Floor((45.4543) * 100) / 100
45.45 = Math.Floor((45.4544) * 100) / 100
45.45 = Math.Floor((45.4545) * 100) / 100
45.45 = Math.Floor((45.4546) * 100) / 100