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


Question

I want to do this using the Math.Round function

1
339
6/26/2009 4:58:24 AM

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.

574
7/23/2018 6:39:59 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon