MidPointRounding for Math.Round() method in .Net Application

Math.Round(doubleValue, precision, MidpointRounding.AwayFromZero)

where

  1. doubleValue is the Decimal valuewhich needs to be Rounded.

  2. precision is the number of significant decimal places(precision) in the return value.

  3. MidPointRounding enumeration value.  

MidPointRounding enumeration has two values ToEven and AwayFromZero.

Default value for MidPointRounding is “ToEven”.

ToEven rounds the value to the nearest even number and AwayFromZero rounds the value to the value which is away from zero.

Given below are some examples using both the Rounding options:-

double result;

result = Math.Round(1.5, 0, MidpointRounding.ToEven); // result = 2

result = Math.Round(2.5, 0,MidpointRounding.ToEven);  //result = 2

result = Math.Round(-2.5, 0, MidpointRounding.ToEven);  //result = -2

result = Math.Round(1.5, 0, MidpointRounding.AwayFromZero);   // result = 2

result = Math.Round(2.5, 0, MidpointRounding.AwayFromZero);   // result = 3

result = Math.Round(-2.5, 0, MidpointRounding.AwayFromZero); // result = -3

Note: This is available for .NET Framework version 2.0 and later.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!