Round Method (Decimal, Int32)

# Decimal.Round Method (Decimal, Int32)

.NET Framework 2.0

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

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

## Syntax

```'Declaration
Public Shared Function Round ( _
d As Decimal, _
decimals As Integer _
) As Decimal
'Usage
Dim d As Decimal
Dim decimals As Integer
Dim returnValue As Decimal

returnValue = Decimal.Round(d, decimals)
```
```public static Decimal Round (
Decimal d,
int decimals
)
```
```public static function Round (
d : decimal,
decimals : int
) : decimal
```

#### Parameters

d

A Decimal value to round.

decimals

A value from 0 to 28 that specifies the number of decimal places to round to.

#### Return Value

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

## Exceptions

Exception typeCondition

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.

## Example

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.
Imports System
Imports Microsoft.VisualBasic

Module DecimalRoundDemo

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

' Display Decimal.Round parameters and the result.
Sub ShowDecimalRound( Argument As Decimal, Digits As Integer )

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

Console.WriteLine( dataFmt, Argument, Digits, rounded )
End Sub

Sub Main( )
Console.WriteLine( "This example of the " & _
"Decimal.Round( Decimal, Integer ) " & vbCrLf & _
"method generates the following output." & vbCrLf )
Console.WriteLine( dataFmt, "Argument", "Digits", "Result" )
Console.WriteLine( dataFmt, "--------", "------", "------" )

' Create pairs of Decimal objects.
ShowDecimalRound( 1.45D, 1 )
ShowDecimalRound( 1.55D, 1 )
ShowDecimalRound( 123.456789D, 4 )
ShowDecimalRound( 123.456789D, 6 )
ShowDecimalRound( 123.456789D, 8 )
ShowDecimalRound( -123.456D, 0 )
ShowDecimalRound( _
New Decimal( 1230000000, 0, 0, True, 7 ), 3 )
ShowDecimalRound( _
New Decimal( 1230000000, 0, 0, True, 7 ), 11 )
ShowDecimalRound( -9999999999.9999999999D, 9 )
ShowDecimalRound( -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.
import System.*;

class DecimalRoundDemo
{
private static String dataFmt = "{0,26}{1,8}{2,26}";

// Display decimal.Round parameters and the result.
public static void ShowDecimalRound(System.Decimal argument, int digits)
{
System.Decimal rounded = System.Decimal.Round(argument, digits);
Console.WriteLine(dataFmt, argument, System.Convert.ToString(digits),
rounded);
} //ShowDecimalRound

public static void main(String[] args)
{
Console.WriteLine("This example of the "
+ "decimal.Round( decimal,Integer ) \n"
+ "method generates the following output.\n");
Console.WriteLine(dataFmt, "argument", "digits", "Result");
Console.WriteLine(dataFmt, "--------", "------", "------");

// Create pairs of decimal objects.
ShowDecimalRound(System.Convert.ToDecimal(1.45), 1);
ShowDecimalRound(System.Convert.ToDecimal(1.55), 1);
ShowDecimalRound(System.Convert.ToDecimal(123.456789), 4);
ShowDecimalRound(System.Convert.ToDecimal(123.456789), 6);
ShowDecimalRound(System.Convert.ToDecimal(123.456789), 8);
ShowDecimalRound(System.Convert.ToDecimal(-123.456), 0);
ShowDecimalRound(new System.Decimal(1230000000, 0, 0, true,
System.Convert.ToByte(7)), 3);
ShowDecimalRound(new System.Decimal(1230000000, 0, 0, true,
System.Convert.ToByte(7)), 11);
ShowDecimalRound(System.Convert.ToDecimal(-9999999999.9999999999), 9);
ShowDecimalRound(System.Convert.ToDecimal(-9999999999.9999999999), 10);
} //main
} //DecimalRoundDemo

/*
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
*/

```

## Platforms

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

## Version Information

#### .NET Framework

Supported in: 2.0, 1.1, 1.0

#### .NET Compact Framework

Supported in: 2.0, 1.0