Rounding:
Code:
double d = 1.499;
double twoDigitsRound = double.Parse(String.Format("{0:0.##}", d)); //1.5, since it's closer to 1.50 than to 1.49...
double fourDigitsRound = double.Parse(String.Format("{0:0.####}", d)); //1.499
//etc...
Flooring:
Code:
Floor(this double d, int numDigits)
{
return (int)(d * Math.Pow(10, numDigits)) / Math.Pow(10, numDigits);
//if Math.Pow(int,int) returns int and not double, make sure you cast it back to double
}
...
double d = 1.499;
double twoDigitsRound = d.Floor(2); //1.49
double fourDigitsRound = d.Floor(4); //1.499
//etc...
Bookmarks