The operation you are looking for is Math.Round
:
Single unrounded = (Single)dr["num1"]; // change if the DB type is not a single
double rounded = Math.Round(unrounded, 3);
Single roundedSingle = (Single)rounded;
Math.Round
returns a double when used with a Single, hence the need to convert to Single (float) afterwards. As a side note: You might want to consider using decimal
s as an alternative, if decimal precision is an issue.
Note that this overload of Math.Round
performs Banker's Rounding, i.e., 0.0005
becomes 0.000
, but 0.0015
becomes 0.002
. If you want a different kind of rounding, use the Math.Round(Double, Int32, MidpointRounding) overload instead.