THIS IS VERY IMPORTANT WHEN ROUNDING NUMBERS IN .NET (C# and VB.NET):
Make sure you use Decimal.Round()
instead of Math.Round()
and be sure to pass in the optional 3rd
parameter MidpointRounding.AwayFromZero
. Otherwise, any decimal
with a "5" in the thousandths place will round down instead of up
because the binary form of the number is smaller than the literal.
CORRECT EXAMPLE (C#):
// Will return 100.91
double num = 100.905;
decimal rounded = Decimal.Round((decimal)num, 2, MidpointRounding.AwayFromZero);
Console.WriteLine(rounded);
INCORRECT EXAMPLE (C#):
// Will return 100.90
double num = 100.905;
double rounded = Math.Round(num, 2);
Console.WriteLine(rounded);