8 January 2008
.NET Math.Round() alternative
Posted by Mikhail Esteves under: C#; Tips .
The Math.Round function in .NET uses the bankers method instead of the mathematical method. So with this new stroke of brilliance, Math.Round(5.5) would give you 5 and Math.Round(5.50001) would give you 6.
Here is a function to do it the traditional way (from here):
public static double RoundNumber(double num, int place) { double n;n = num * Math.Pow(10, place); n = Math.Sign(n) * Math.Abs(Math.Floor(n + .5)); return n / Math.Pow(10, place); }