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


Decimal.Round Method

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

Rounds a Decimal value to a specified number of decimal places.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Round ( _
    d As Decimal, _
    decimals As Integer _
) As Decimal
[SecuritySafeCriticalAttribute]
public static decimal Round(
    decimal d,
    int decimals
)

Parameters

  • decimals
    Type: System.Int32
    A value from 0 to 28 that specifies the number of decimal places to round to.

Return Value

Type: System.Decimal
The Decimal number equivalent to d rounded to decimals number of decimal places.

Exceptions

Exception Condition
ArgumentOutOfRangeException

decimals is not a value from 0 to 28.

Remarks

When d is exactly halfway between two rounded values, the result is the rounded value that has an even digit in the far right decimal position. For example, when rounded to two decimals, the value 2.345 becomes 2.34 and the value 2.355 becomes 2.36. This process is known as rounding toward even, or rounding to nearest.

Examples

The following code example rounds several Decimal values to a specified number of decimal places using the Round method.

' Example of the Decimal.Round method. 

Module Example

   Const dataFmt As String = "{0,26}{1,8}{2,26}"

   ' Display Decimal.Round parameters and the result.
   Sub ShowDecimalRound(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Argument As Decimal, ByVal Digits As Integer)

      Dim rounded As Decimal = Decimal.Round(Argument, Digits)

      outputBlock.Text &= String.Format(dataFmt, Argument, Digits, rounded) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= "This example of the " & _
          "Decimal.Round( Decimal, Integer ) " & vbCrLf & _
          "method generates the following output." & vbCrLf & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "Argument", "Digits", "Result") & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "--------", "------", "------") & vbCrLf

      ' Create pairs of Decimal objects.
      ShowDecimalRound(outputBlock, 1.45D, 1)
      ShowDecimalRound(outputBlock, 1.55D, 1)
      ShowDecimalRound(outputBlock, 123.456789D, 4)
      ShowDecimalRound(outputBlock, 123.456789D, 6)
      ShowDecimalRound(outputBlock, 123.456789D, 8)
      ShowDecimalRound(outputBlock, -123.456D, 0)
      ShowDecimalRound(outputBlock,  _
          New Decimal(1230000000, 0, 0, True, 7), 3)
      ShowDecimalRound(outputBlock,  _
          New Decimal(1230000000, 0, 0, True, 7), 11)
      ShowDecimalRound(outputBlock, -9999999999.9999999999D, 9)
      ShowDecimalRound(outputBlock, -9999999999.9999999999D, 10)
   End Sub
End Module

' This example of the Decimal.Round( Decimal, Integer )
' method generates the following output.
'
'                  Argument  Digits                    Result
'                  --------  ------                    ------
'                      1.45       1                       1.4
'                      1.55       1                       1.6
'                123.456789       4                  123.4568
'                123.456789       6                123.456789
'                123.456789       8                123.456789
'                  -123.456       0                      -123
'              -123.0000000       3                  -123.000
'              -123.0000000      11              -123.0000000
'    -9999999999.9999999999       9    -10000000000.000000000
'    -9999999999.9999999999      10    -9999999999.9999999999
// Example of the decimal.Round method. 
using System;

class Example
{
   const string dataFmt = "{0,26}{1,8}{2,26}";

   // Display decimal.Round parameters and the result.
   public static void ShowDecimalRound(System.Windows.Controls.TextBlock outputBlock, decimal Argument, int Digits)
   {
      decimal rounded = decimal.Round(Argument, Digits);

      outputBlock.Text += String.Format(dataFmt, Argument, Digits, rounded) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "This example of the " +
          "decimal.Round( decimal, Integer ) \n" +
          "method generates the following output.\n" + "\n";
      outputBlock.Text += String.Format(dataFmt, "Argument", "Digits", "Result") + "\n";
      outputBlock.Text += String.Format(dataFmt, "--------", "------", "------") + "\n";

      // Create pairs of decimal objects.
      ShowDecimalRound(outputBlock, 1.45M, 1);
      ShowDecimalRound(outputBlock, 1.55M, 1);
      ShowDecimalRound(outputBlock, 123.456789M, 4);
      ShowDecimalRound(outputBlock, 123.456789M, 6);
      ShowDecimalRound(outputBlock, 123.456789M, 8);
      ShowDecimalRound(outputBlock, -123.456M, 0);
      ShowDecimalRound(outputBlock, 
          new decimal(1230000000, 0, 0, true, 7), 3);
      ShowDecimalRound(outputBlock, 
          new decimal(1230000000, 0, 0, true, 7), 11);
      ShowDecimalRound(outputBlock, -9999999999.9999999999M, 9);
      ShowDecimalRound(outputBlock, -9999999999.9999999999M, 10);
   }
}

/*
This example of the decimal.Round( decimal, Integer )
method generates the following output.

                  Argument  Digits                    Result
                  --------  ------                    ------
                      1.45       1                       1.4
                      1.55       1                       1.6
                123.456789       4                  123.4568
                123.456789       6                123.456789
                123.456789       8                123.456789
                  -123.456       0                      -123
              -123.0000000       3                  -123.000
              -123.0000000      11              -123.0000000
    -9999999999.9999999999       9    -10000000000.000000000
    -9999999999.9999999999      10    -9999999999.9999999999
*/

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.