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);
} 


Leave a Reply

Browse

Photography

Projects

Pages

Calendar

January 2008
M T W T F S S
« Dec   Feb »
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories

www.flickr.com