The value 18.0051 will ALWAYS round to 18.01. To do what you want, you could use string manipulation:
double value = 18.0051;
string str = value.ToString();
int pos = Math.Min(str.Length, str.IndexOf(".") + 3);
if (pos > 0)
{
str = str.Substring(0, pos);
}
value = Convert.ToDouble(str);
You could put that code into an extension method so it's always available.
public static double Truncate(this double value, int places)
{
double result = value;
string str = value.ToString();
int pos = Math.Min(str.Length, str.IndexOf(".") + places + 1);
if (pos > 0)
{
str = str.Substring(0, pos);
}
result = Convert.ToDouble(str);
return result;
}
Using the new extension method would reduce your outward facing code to a single line:
double value = 18.0051;
value = value.Truncate(2);