Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Decimal::Round Method (Decimal, Int32)

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

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

public:
static Decimal Round(
	Decimal d, 
	int decimals
)

Parameters

d
Type: System::Decimal

A decimal number to round.

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.

ExceptionCondition
ArgumentOutOfRangeException

decimals is not a value from 0 to 28.

This method is equivalent to calling the Round(Decimal, Int32, MidpointRounding) method with a mode argument of MidpointRounding::ToEven.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. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

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.  
using namespace System;
#define dataFmt "{0,26}{1,8}{2,26}" 

// Display Decimal::Round parameters and the result. 
void ShowDecimalRound( Decimal Argument, int Digits )
{
   Decimal rounded = Decimal::Round( Argument, Digits );
   Console::WriteLine( dataFmt, Argument, Digits, rounded );
}

int main()
{
   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( Decimal::Parse( "1.45" ), 1 );
   ShowDecimalRound( Decimal::Parse( "1.55" ), 1 );
   ShowDecimalRound( Decimal::Parse( "123.456789" ), 4 );
   ShowDecimalRound( Decimal::Parse( "123.456789" ), 6 );
   ShowDecimalRound( Decimal::Parse( "123.456789" ), 8 );
   ShowDecimalRound( Decimal::Parse( "-123.456" ), 0 );
   ShowDecimalRound( Decimal(1230000000,0,0,true,7), 3 );
   ShowDecimalRound( Decimal(1230000000,0,0,true,7), 11 );
   ShowDecimalRound( Decimal::Parse( "-9999999999.9999999999" ), 9 );
   ShowDecimalRound( Decimal::Parse( "-9999999999.9999999999" ), 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
*/

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Obsolete (compiler warning) in Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft