Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
4 out of 7 rated this helpful - Rate this topic

Math.Round Method

Rounds a value to the nearest integer or to the specified number of fractional digits.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method Static member Round(Decimal) Rounds a decimal value to the nearest integral value.
Public method Static member Round(Double) Rounds a double-precision floating-point value to the nearest integral value.
Public method Static member Round(Decimal, Int32) Rounds a decimal value to a specified number of fractional digits.
Public method Static member Round(Decimal, MidpointRounding) Rounds a decimal value to the nearest integer. A parameter specifies how to round the value if it is midway between two numbers.
Public method Static member Round(Double, Int32) Rounds a double-precision floating-point value to a specified number of fractional digits.
Public method Static member Round(Double, MidpointRounding) Rounds a double-precision floating-point value to the nearest integer. A parameter specifies how to round the value if it is midway between two numbers.
Public method Static member Round(Decimal, Int32, MidpointRounding) Rounds a decimal value to a specified number of fractional digits. A parameter specifies how to round the value if it is midway between two numbers.
Public method Static member Round(Double, Int32, MidpointRounding) Rounds a double-precision floating-point value to a specified number of fractional digits. A parameter specifies how to round the value if it is midway between two numbers.
Top

In this section:

Overloaded method syntax
Parameters
Return value
Exceptions
Which method do I call?
Midpoint values and rounding conventions
Rounding and precision
Rounding and single-precision floating-point values
Examples:
       Math.Round(Decimal)
       Math.Round(Double)
       Math.Round(Decimal, Int32)
       Math.Round(Decimal, MidpointRounding)
       Math.Round(Double, Int32)
       Math.Round(Double, MidpointRounding)
       Math.Round(Decimal, Int32, MidpointRounding)
       Math.Round(Double, Int32, MidpointRounding)
Version information

Overloaded method syntax

For additional guidance on choosing an overload, see Which method do I call?

Decimal Math.Round(Decimal d )

Rounds a decimal value to the nearest integer, and rounds midpoint values to the nearest even number (example).

Double Math.Round(Double a )

Rounds a double-precision floating-point value to the nearest integer, and rounds midpoint values to the nearest even number (example).

Decimal Math.Round(Decimal d , Int32 decimals )

Rounds a decimal value to a specified number of fractional digits, and rounds midpoint values to the nearest even number (example).

Double Math.Round(Double value , Int32 digits )

Rounds a double-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number (example).

Decimal Math.Round(Decimal d , MidpointRounding mode )

Rounds a decimal value to the nearest integer, and uses the specified rounding convention for midpoint values (example).

Double Math.Round(Double value , MidpointRounding mode )

Rounds a double-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values (example).

Decimal Math.Round(Decimal d , Int32 decimals , MidpointRounding mode )

Rounds a decimal value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values (example).

Double Math.Round(Double value , Int32 digits , MidpointRounding mode )

Rounds a double-precision floating-point value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values (example).

Parameters

This is a complete list of parameters for the Round method. For the parameters used by each overload, see the overload syntax above.

Parameter

Type

Description

d

Decimal

A number to be rounded.

a

-or-

value

Double

A double-precision floating-point number to be rounded.

decimals

-or-

digits

Int32

The number of fractional digits in the return value. For Decimal values, it can range from 0 to 28. For Double values, it can range from 0 to 15. Note that 15 is the maximum number of integral and fractional digits supported by the Double type.

Default value: 0, which rounds to an integer

mode

MidpointRounding

A flag that specifies the convention to use for rounding midpoint values. See Midpoint values and rounding conventions.

Default value: MidpointRounding.ToEven

Return value

Type: Decimal or Double, depending on the type of the value to be rounded.
A numeric value of the same type as d, a, or value, but rounded either to an integer or to the specified number of decimal digits. See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Caution note Caution

When rounding midpoint values, the rounding algorithm performs an equality test. Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. For more information, see Rounding and precision.

If an overload with a Double parameter is called with Double.NaN, the method returns Double.NaN. If it is called with Double.PositiveInfinity or Double.NegativeInfinity, the method returns Double.PositiveInfinity or Double.NegativeInfinity, respectively.

Exceptions

Which method do I call?

To

Call

Round a number to an integer by using the rounding to nearest convention.

Round(Decimal)

-or-

Round(Double)

Round a number to an integer by using a specified rounding convention.

Round(Decimal, MidpointRounding)

-or-

Round(Double, MidpointRounding)

Round a number to a specified number of fractional digits by using the rounding to nearest convention.

Round(Decimal, Int32)

-or-

Round(Double, Int32)

Round a number to a specified number of fractional digits by using a specified rounding convention.

Round(Decimal, Int32, MidpointRounding)

-or-

Round(Double, Int32, MidpointRounding)

Round a Single value to a specified number of fractional digits by using a specified rounding convention and minimizing the loss of precision.

Convert the Single to a Decimal and call Round(Decimal, Int32, MidpointRounding).

Round a number to a specified number of fractional digits while minimizing problems of precision in rounding midpoint values.

Call a rounding method that implements a "greater than or approximately equal to" comparison. See Rounding and precision.

Round a fractional value to an integer that is greater than the fractional value. For example, round 3.1 to 4.

Ceiling

Round a fractional value to an integer that is less than the fractional value. For example, round 3.9 to 3.

Floor

Midpoint values and rounding conventions

Rounding involves converting a numeric value with a specified precision to the nearest value with less precision. For example, you can use the Round(Double) method to round a value of 3.4 to 3.0, and the Round(Double, Int32) method to round a value of 3.579 to 3.58.

In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 3.47500 is a midpoint value if it is to be rounded two decimal places, and 7.500 is a midpoint value if it is to be rounded to an integer. In these cases, the nearest value can't be easily identified without a rounding convention.

The Round method supports two rounding conventions for handling midpoint values:

Rounding away from zero

Midpoint values are rounded to the next number away from zero. For example, 3.75 rounds to 3.8, 3.85 rounds to 3.9, -3.75 rounds to -3.8, and -3.85 rounds to -3.9. This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.

Rounding away from zero is the most widely known form of rounding.

Rounding to nearest, or banker's rounding

Midpoint values are rounded to the nearest even number. For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. This form of rounding is represented by the MidpointRounding.ToEven enumeration member.

Rounding to nearest is the standard form of rounding used in financial and statistical operations. It conforms to IEEE Standard 754, section 4. When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. In some cases, this rounding error can be significant.

The following example illustrates the bias that can result from consistently rounding midpoint values in a single direction. The example computes the true mean of an array of Decimal values, and then computes the mean when the values in the array are rounded by using the two conventions. In this example, the true mean and the mean that results when rounding to nearest are the same. However, the mean that results when rounding away from zero differs by .05 (or by 3.6%) from the true mean.

using System;

public class Example
{
   public static void Main()
   {
      decimal[] values = { 1.15m, 1.25m, 1.35m, 1.45m, 1.55m, 1.65m };
      decimal sum = 0;

      // Calculate true mean. 
      foreach (var value in values)
         sum += value;

      Console.WriteLine("True mean:     {0:N2}", sum/values.Length);

      // Calculate mean with rounding away from zero.
      sum = 0;
      foreach (var value in values)
         sum += Math.Round(value, 1, MidpointRounding.AwayFromZero);

      Console.WriteLine("AwayFromZero:  {0:N2}", sum/values.Length);

      // Calculate mean with rounding to nearest.
      sum = 0;
      foreach (var value in values)
         sum += Math.Round(value, 1, MidpointRounding.ToEven);

      Console.WriteLine("ToEven:        {0:N2}", sum/values.Length);
   }
}
// The example displays the following output: 
//       True mean:     1.40 
//       AwayFromZero:  1.45 
//       ToEven:        1.40

By default, the Round method uses the rounding to nearest convention. The following table lists the overloads of the Round method and the rounding convention that each uses.

Overload

Rounding convention

Round(Decimal)

ToEven

Round(Double)

ToEven

Round(Decimal, Int32)

ToEven

Round(Double, Int32)

ToEven

Round(Decimal, MidpointRounding)

Determined by mode parameter.

Round(Double, MidpointRounding)

Determined by mode parameter

Round(Decimal, Int32, MidpointRounding)

Determined by mode parameter

Round(Double, Int32, MidpointRounding)

Determined by mode parameter

Rounding and precision

In order to determine whether a rounding operation involves a midpoint value, the Round method multiplies the original value to be rounded by 10n, where n is the desired number of fractional digits in the return value, and then determines whether the remaining fractional portion of the value is greater than or equal to .5. This is a slight variation on a test for equality, and as discussed in the "Testing for Equality" section of the Double reference topic, tests for equality with floating-point values are problematic because of the floating-point format's issues with binary representation and precision. This means that any fractional portion of a number that is slightly less than .5 (because of a loss of precision) will not be rounded upward.

The following example illustrates the problem. It repeatedly adds .1 to 11.0 and rounds the result to the nearest integer. Regardless of the rounding convention, 11.5 should round to 12. However, as the output from the example shows, it does not. The example uses the "R" standard numeric format string to display the floating point value's full precision, and shows that the value to be rounded has lost precision during repeated additions, and its value is actually 11.499999999999998. Because .499999999999998 is less than .5, the value is not rounded to the next highest integer. As the example also shows, this problem does not occur if we simply assign the constant value 11.5 to a Double variable.

using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,5} {1,20:R}  {2,12} {3,15}\n", 
                        "Value", "Full Precision", "ToEven",
                        "AwayFromZero");
      double value = 11.1;
      for (int ctr = 0; ctr <= 5; ctr++)    
         value = RoundValueAndAdd(value);

      Console.WriteLine();

      value = 11.5;
      RoundValueAndAdd(value);
   }

   private static double RoundValueAndAdd(double value)
   {
      Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}", 
                        value, Math.Round(value, MidpointRounding.ToEven),
                        Math.Round(value, MidpointRounding.AwayFromZero));
      return value + .1;
   }
}
// The example displays the following output: 
//       Value       Full Precision        ToEven    AwayFromZero 
//        
//        11.1                 11.1            11              11 
//        11.2                 11.2            11              11 
//        11.3   11.299999999999999            11              11 
//        11.4   11.399999999999999            11              11 
//        11.5   11.499999999999998            11              11 
//        11.6   11.599999999999998            12              12 
//        
//        11.5                 11.5            12              12

Problems of precision in rounding midpoint values are most likely to arise in the following conditions:

  • When a fractional value cannot be expressed precisely in the floating-point type's binary format.

  • When the value to be rounded is calculated from one or more floating-point operations.

  • When the value to be rounded is a Single rather than a Double or Decimal. For more information, see the next section, Rounding and single-precision floating-point values.

In cases where the lack of precision in rounding operations is problematic, you can do the following:

  • If the rounding operation calls an overload that rounds a Double value, you can change the Double to a Decimal value and call an overload that rounds a Decimal value instead. Although the Decimal data type also has problems of representation and loss of precision, these issues are far less common.

  • Define a custom rounding algorithm that performs a "nearly equal" test to determine whether the value to be rounded is acceptably close to a midpoint value. The following example defines a RoundApproximate method that examines whether a fractional value is sufficiently near to a midpoint value to be subject to midpoint rounding. As the output from the example shows, it corrects the rounding problem shown in the previous example.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Console.WriteLine("{0,5} {1,20:R}  {2,12} {3,15}\n", 
                            "Value", "Full Precision", "ToEven",
                            "AwayFromZero");
          double value = 11.1;
          for (int ctr = 0; ctr <= 5; ctr++)    
             value = RoundValueAndAdd(value);
    
          Console.WriteLine();
    
          value = 11.5;
          RoundValueAndAdd(value);
       }
    
       private static double RoundValueAndAdd(double value)
       {
          const double tolerance = 8e-14;
    
          Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}", 
                            value, 
                            RoundApproximate(value, 0, tolerance, MidpointRounding.ToEven),
                            RoundApproximate(value, 0, tolerance, MidpointRounding.AwayFromZero));
          return value + .1;
       }
    
       private static double RoundApproximate(double dbl, int digits, double margin, 
                                         MidpointRounding mode)
       {                                      
          double fraction = dbl * Math.Pow(10, digits);
          double value = Math.Truncate(fraction); 
          fraction = fraction - value;   
          if (fraction == 0)
             return dbl;
    
          double tolerance = margin * dbl;
          // Determine whether this is a midpoint value. 
          if ((fraction >= .5 - tolerance) & (fraction <= .5 + tolerance)) {
             if (mode == MidpointRounding.AwayFromZero)
                return (value + 1)/Math.Pow(10, digits);
             else 
                if (value % 2 != 0)
                   return (value + 1)/Math.Pow(10, digits);
                else 
                   return value/Math.Pow(10, digits);
          }
          // Any remaining fractional value greater than .5 is not a midpoint value. 
          if (fraction > .5)
             return (value + 1)/Math.Pow(10, digits);
          else 
             return value/Math.Pow(10, digits);
       }
    }
    // The example displays the following output: 
    //       Value       Full Precision        ToEven    AwayFromZero 
    //        
    //        11.1                 11.1            11              11 
    //        11.2                 11.2            11              11 
    //        11.3   11.299999999999999            11              11 
    //        11.4   11.399999999999999            11              11 
    //        11.5   11.499999999999998            12              12 
    //        11.6   11.599999999999998            12              12 
    //        
    //        11.5                 11.5            12              12
    

Rounding and single-precision floating-point values

The Round method includes overloads that accept arguments of type Decimal and Double. There are no methods that round values of type Single. If you pass a Single value to one of the overloads of the Round method, it is cast (in C#) or converted (in Visual Basic) to a Double, and the corresponding Round overload with a Double parameter is called. Although this is a widening conversion, it often involves a loss of precision, as the following example illustrates. When a Single value of 16.325 is passed to the Round method and rounded to two decimal places using the rounding to nearest convention, the result is 16.33 and not the expected result of 16.32.

using System;

public class Example
{
   public static void Main()
   {
      Single value = 16.325f;
      Console.WriteLine("Widening Conversion of {0:R} (type {1}) to {2:R} (type {3}): ", 
                        value, value.GetType().Name, (double) value, 
                        ((double) (value)).GetType().Name);
      Console.WriteLine(Math.Round(value, 2));
      Console.WriteLine(Math.Round(value, 2, MidpointRounding.AwayFromZero));
      Console.WriteLine();

      Decimal decValue = (decimal) value;
      Console.WriteLine("Cast of {0:R} (type {1}) to {2} (type {3}): ", 
                        value, value.GetType().Name, decValue, 
                        decValue.GetType().Name);
      Console.WriteLine(Math.Round(decValue, 2));
      Console.WriteLine(Math.Round(decValue, 2, MidpointRounding.AwayFromZero));
   }
}
// The example displays the following output: 
//    Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double): 
//    16.33 
//    16.33 
//     
//    Cast of 16.325 (type Single) to 16.325 (type Decimal): 
//    16.32 
//    16.33

This unexpected result is due to a loss of precision in the conversion of the Single value to a Double. Because the resulting Double value of 16.325000762939453 is not a midpoint value and is greater than 16.325, it is always rounded upward.

In many cases, as the example illustrates, the loss of precision can be minimized or eliminated by casting or converting the Single value to a Decimal. Note that, because this is a narrowing conversion, it requires using a cast operator or calling a conversion method.

Math.Round(Decimal) example

The following example demonstrates the Round(Decimal) method. Note that the Decimal value of 4.5 rounds to 4 rather than 5, because this overload uses the default ToEven convention.

using System;

class Example
{
   static void Main()
   {
      for (decimal value = 4.2m; value <= 4.8m; value+=.1m )
         Console.WriteLine("{0} --> {1}", value, Math.Round(value));
   }
}
// The example displays the following output: 
//       4.2 --> 4 
//       4.3 --> 4 
//       4.4 --> 4 
//       4.5 --> 4 
//       4.6 --> 5 
//       4.7 --> 5 
//       4.8 --> 5

Math.Round(Double) example

The following example demonstrates rounding to the nearest integer value.

using System;

class Program
{
    static void Main()
    {
    Console.WriteLine("Classic Math.Round in CSharp");
    Console.WriteLine(Math.Round(4.4)); // 4
    Console.WriteLine(Math.Round(4.5)); // 4
    Console.WriteLine(Math.Round(4.6)); // 5
    Console.WriteLine(Math.Round(5.5)); // 6
    }
}

Math.Round(Decimal, Int32) example

The following example rounds decimal values with two fractional digits to values that have a single fractional digit.

using System;

public class Sample {
   static void Main() {
      Console.WriteLine(Math.Round(3.44m, 1)); 
      Console.WriteLine(Math.Round(3.45m, 1)); 
      Console.WriteLine(Math.Round(3.46m, 1)); 
      Console.WriteLine();

      Console.WriteLine(Math.Round(4.34m, 1)); 
      Console.WriteLine(Math.Round(4.35m, 1)); 
      Console.WriteLine(Math.Round(4.36m, 1)); 
   }
}
// The example displays the following output: 
//       3.4 
//       3.4 
//       3.5 
//        
//       4.3 
//       4.4 
//       4.4

Math.Round(Double, Int32) example

The following example rounds double values with two fractional digits to doubles that have a single fractional digit.

Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.

Math.Round(4.34, 1); // Returns 4.3
Math.Round(4.35, 1); // Returns 4.4
Math.Round(4.36, 1); // Returns 4.4

Math.Round(Decimal, MidpointRounding) example

The following example displays values returned by the Round(Decimal) method, the Round(Decimal, MidpointRounding) method with a mode argument of ToEven, and the Round(Decimal, MidpointRounding) method with a mode argument of AwayFromZero.

using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                        "ToEven", "AwayFromZero");
      for (decimal value = 12.0m; value <= 13.0m; value += 0.1m)
         Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value), 
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero));
   }
}
// The example displays the following output: 
//       Value      Default    ToEven     AwayFromZero 
//       12         12         12         12 
//       12.1       12         12         12 
//       12.2       12         12         12 
//       12.3       12         12         12 
//       12.4       12         12         12 
//       12.5       12         12         13 
//       12.6       13         13         13 
//       12.7       13         13         13 
//       12.8       13         13         13 
//       12.9       13         13         13 
//       13.0       13         13         13

Math.Round(Double, MidpointRounding) example

The following example displays values returned by the Round(Double) method, the Round(Double, MidpointRounding) method with a mode argument of ToEven, and the Round(Double, MidpointRounding) method with a mode argument of AwayFromZero.

using System;

public class Example
{
   public static void Main()
   {
      Double[] values = { 12.0, 12.1, 12.2, 12.3, 12.4, 12.5, 12.6, 
                          12.7, 12.8, 12.9, 13.0 };
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                        "ToEven", "AwayFromZero");
      foreach (var value in values)
         Console.WriteLine("{0,-10:R} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value), 
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero));
   }
}
// The example displays the following output: 
//       Value      Default    ToEven     AwayFromZero 
//       12         12         12         12 
//       12.1       12         12         12 
//       12.2       12         12         12 
//       12.3       12         12         12 
//       12.4       12         12         12 
//       12.5       12         12         13 
//       12.6       13         13         13 
//       12.7       13         13         13 
//       12.8       13         13         13 
//       12.9       13         13         13 
//       13.0       13         13         13

Math.Round(Decimal, Int32, MidpointRounding) example

The following example demonstrates how to use the Round method with the MidpointRounding enumeration.

// This example demonstrates the Math.Round() method in conjunction  
// with the MidpointRounding enumeration. 
using System;

class Sample 
{
    public static void Main() 
    {
    decimal result = 0.0m;
    decimal posValue =  3.45m;
    decimal negValue = -3.45m;

// By default, round a positive and a negative value to the nearest even number.  
// The precision of the result is 1 decimal place.

    result = Math.Round(posValue, 1);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
    result = Math.Round(negValue, 1);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
    Console.WriteLine();

// Round a positive value to the nearest even number, then to the nearest number away from zero.  
// The precision of the result is 1 decimal place.

    result = Math.Round(posValue, 1, MidpointRounding.ToEven);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
    result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
    Console.WriteLine();

// Round a negative value to the nearest even number, then to the nearest number away from zero.  
// The precision of the result is 1 decimal place.

    result = Math.Round(negValue, 1, MidpointRounding.ToEven);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
    result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
    Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
    Console.WriteLine();
    }
}
/*
This code example produces the following results:

 3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

*/

Math.Round(Double, Int32, MidpointRounding) example

The following example demonstrates how to use the Round(Double, Int32, MidpointRounding) method with the MidpointRounding enumeration.

using System;

class Sample 
{
    public static void Main() 
    {
       double posValue =  3.45;
       double negValue = -3.45;

       // Round a positive and a negative value using the default.  
       double result = Math.Round(posValue, 1);
       Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
       result = Math.Round(negValue, 1);
       Console.WriteLine("{0,4} = Math.Round({1,5}, 1)\n", result, negValue);

       // Round a positive value using a MidpointRounding value. 
       result = Math.Round(posValue, 1, MidpointRounding.ToEven);
       Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", 
                         result, posValue);
       result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
       Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)\n", 
                         result, posValue);

       // Round a negative value using a MidpointRounding value. 
       result = Math.Round(negValue, 1, MidpointRounding.ToEven);
       Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", 
                         result, negValue);
       result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
       Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)\n", 
                         result, negValue);
   }
}
// The example displays the following output: 
//        3.4 = Math.Round( 3.45, 1) 
//       -3.4 = Math.Round(-3.45, 1) 
//        
//        3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven) 
//        3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero) 
//        
//       -3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven) 
//       -3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

Version information

.NET Framework

Overloads without a MidpointRounding parameter are supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
Overloads with a MidpointRounding parameter are supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

All overloads are supported in: 4, 3.5 SP1

Portable Class Library

All overloads are supported

.NET for Windows Store apps

All overloads are supported in: Windows 8

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.