Round a number to the nearest even number in C#

CsharpProgrammingServer Side Programming

The ToEven property is used with the MidpointRounding Enumeration to round a number to the nearest even number.

Declare and initialize a decimal number −

decimal val = 25.55M;

To round a number to the nearest even number −

decimal.Round(val, 0, MidpointRounding.ToEven)

Here is the complete code −

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      decimal val = 25.55M;
      Console.WriteLine(decimal.Round(val, 0, MidpointRounding.ToEven));
   }
}

Output

26
raja
Published on 05-Sep-2018 14:14:31
Advertisements