Math.Round Method (Decimal)

May 11, 2014

Rounds a decimal value to the nearest integral value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static decimal Round(
	decimal d
)

Parameters

d
Type: System.Decimal
A decimal number to be rounded.

Return Value

Type: System.Decimal
The integer nearest parameter d. If the fractional component of d is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that the method returns a Decimal type rather than an integral type.

ExceptionCondition
OverflowException

The result is outside the range of a Decimal.

The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

The following example demonstrates rounding to nearest.


using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Classic Math.Round in CSharp" + "\n";
      outputBlock.Text += Math.Round(4.4) + "\n"; // 4
      outputBlock.Text += Math.Round(4.5) + "\n"; // 4
      outputBlock.Text += Math.Round(4.6) + "\n"; // 5
      outputBlock.Text += Math.Round(5.5) + "\n"; // 6
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft