Mono Class Library: System.Decimal Overview | Members

System.Decimal.Round Method

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

public static decimal Round (decimal d, int decimals)

Parameters

d
The decimal to round.
decimals
The number of decimal places to round to. 0 <= decimals <= 28.

Returns

The decimal result of rounding d to decimals decimal places.

Exceptions

TypeReason
ArgumentOutOfRangeExceptiondecimals is not between 0 and 28, inclusive.

Remarks

When d is exactly half way between two rounded values, the result is the rounded value that has an even digit in the rightmost decimal position. For example, when rounded to two decimals, the value 2.345 becomes 2.34 and the value 2.355 becomes 2.36.

Note: This process is known as rounding half towards even, or banker's rounding.

The scale of the result will be the smaller of decimals and the scale of d.

Note: The scale of d is never increased, so decimal.Round(decimal, int) cannot cause overflow.

Example

The following example demonstrates the decimal.Round(decimal, int) method.

C# Example

using System;
class MyClass {
public static void Main() {
 decimal d1 = 2.5m;
 decimal d2 = 3.5m;
 decimal d3 = 2.98765432m;
 decimal d4 = 2.18765432m;
 Console.WriteLine("Rounding to 0 places...");
 Console.WriteLine("round {0} = {1}",d1, Decimal.Round(d1,0));
 Console.WriteLine("round {0} = {1}",d2, Decimal.Round(d2,0));
 Console.WriteLine("round {0} = {1}",d3, Decimal.Round(d3,0));
 Console.WriteLine("round {0} = {1}",d4, Decimal.Round(d4,0));
 Console.WriteLine("Rounding to 2 places...");
 Console.WriteLine("round {0} = {1}",d1, Decimal.Round(d1,2));
 Console.WriteLine("round {0} = {1}",d2, Decimal.Round(d2,2));
 Console.WriteLine("round {0} = {1}",d3, Decimal.Round(d3,2));
 Console.WriteLine("round {0} = {1}",d4, Decimal.Round(d4,2));
}
}

The output is

Rounding to 0 places...
round 2.5 = 2
round 3.5 = 4
round 2.98765432 = 3
round 2.18765432 = 2
Rounding to 2 places...
round 2.5 = 2.5
round 3.5 = 3.5
round 2.98765432 = 2.99
round 2.18765432 = 2.19

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0