How do you round a number to two decimal places in C#?

Laila picture Laila · Nov 2, 2008 · Viewed 618.4k times · Source

I want to do this using the Math.Round function

Answer

Eoin Campbell picture Eoin Campbell · Nov 2, 2008

Here's some examples:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

You might also want to look at bankers rounding / round-to-even with the following overload:

Math.Round(a, 2, MidpointRounding.ToEven);

There's more information on it here.