Wednesday, May 10, 2006

A Rounding Function That Really Works

public static double RoundDouble(double number,int digits)
{

string Temp;

string Temp2;

int i, j;

double ResultValue;

double Numbertemp;

Temp = Convert.ToString(number);

i = Temp.LastIndexOf(".");

if (((Temp.Length - (i + 1)) <= digits) (i == -1)) return number; Temp2 = Temp.Substring(i + digits + 1, 1); j = Convert.ToInt32(Temp2); Numbertemp = Convert.ToDouble(Temp.Substring(0, i + digits + 1)); if (j == 5) ResultValue = Numbertemp + (1 / (Math.Pow(10,digits))); else ResultValue = Math.Round(number, digits); return ResultValue; } Courtesy of

vedapuri

http://www.codeproject.com/useritems/Solving_rounding_problem.asp

No comments:

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Z