Поделиться через


Math.Round Method (Decimal)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Rounds a decimal value to the nearest integral value.

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

Syntax

'Declaration
Public Shared Function Round ( _
    d As Decimal _
) As Decimal
public static decimal Round(
    decimal d
)

Parameters

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.

Exceptions

Exception Condition
OverflowException

The result is outside the range of a Decimal.

Remarks

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.

Examples

The following example demonstrates rounding to nearest.


Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= "Classic Math.Round in Visual Basic" & vbCrLf
      outputBlock.Text &= Math.Round(4.4) & vbCrLf ' 4
      outputBlock.Text &= Math.Round(4.5) & vbCrLf ' 4
      outputBlock.Text &= Math.Round(4.6) & vbCrLf ' 5
      outputBlock.Text &= Math.Round(5.5) & vbCrLf ' 6
   End Sub

End Module
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
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.