Wednesday, May 24, 2006

Mod It

No, I'm not talking about altering your automobile, or bending your bike (what?). No, I'm talking about the good old fashioned Mod function of course. It took me a while to realise that the equivalent in C# is...

int result = firstNumber % secondNumber;

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

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