C# program that uses Math.Roundusing System; class Program { static void Main() {// // Round double type in three ways. //double before1 = 123.45; double after1 =Math.Round(before1, 1, MidpointRounding.AwayFromZero);// Rounds "up"double after2 =Math.Round(before1, 1, MidpointRounding.ToEven);// Rounds to evendouble after3 =Math.Round(before1); Console.WriteLine(before1);// OriginalConsole.WriteLine(after1); Console.WriteLine(after2); Console.WriteLine(after3);// // Round decimal type. //decimal before2 = 125.101M; decimal after4 =Math.Round(before2); decimal after5 =Math.Round(before2, 1); Console.WriteLine(before2);// OriginalConsole.WriteLine(after4); Console.WriteLine(after5); } }Output123.45 123.5 <-- MidpointRounding.AwayFromZero 123.4 <-- MidpointRounding.ToEven 123 125.101 125 125.1

**Then:**The program shows three outputs of Math.Round on a double with value 123.45.

**Info:**The Math.Round call that specifies one decimal place and the MidpointRounding.AwayFromZero constant rounds that value up to 123.5.

**And:**The Math.Round call with one decimal place and the MidpointRounding.ToEven constant rounds down to 123.4. The result is even.

C# program that demonstrates MidpointRoundingusing System; class Program { static void Main() { for (double i = 0.1; i < 0.99; i += 0.1) { Console.WriteLine("{0}=({1},{2})", i, Math.Round(i, MidpointRounding.ToEven), Math.Round(i, MidpointRounding.AwayFromZero)); } } }Output0.1=(0,0) 0.2=(0,0) 0.3=(0,0) 0.4=(0,0) 0.5=(0,1) 0.6=(1,1) 0.7=(1,1) 0.8=(1,1) 0.9=(1,1)

**Note:**This article had an error in its description of rounding behaviors on scientific data. Thanks to Gus Gustafson for a correction.

C# program that benchmarks Math.Roundusing System; using System.Diagnostics; class Program { const int _max = 100000000; static void Main() { var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { double d = Math.Round(1.3665, 0); if (d == 1.5) { throw new Exception(); } } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { double d = Math.Round(1.3665, 1); if (d == 1.5) { throw new Exception(); } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } }Output20.72 ns 25.26 ns