C# Math Round(Decimal, Int32, MidpointRounding)
Description
Math Round(Decimal, Int32, MidpointRounding)
rounds
a decimal value to a specified number of fractional digits. A parameter specifies
how to round the value if it is midway between two numbers.
Syntax
Math.Round(Decimal, Int32, MidpointRounding)
has the following syntax.
public static decimal Round(
decimal d,// w w w . j a v a2 s . c om
int decimals,
MidpointRounding mode
)
Parameters
Math.Round(Decimal, Int32, MidpointRounding)
has the following parameters.
d
- A decimal number to be rounded.decimals
- The number of decimal places in the return value.mode
- Specification for how to round d if it is midway between two other numbers.
Returns
Math.Round(Decimal, Int32, MidpointRounding)
method returns The number nearest to d that contains a number of fractional digits equal
to decimals. If d has fewer fractional digits than decimals, d is returned
unchanged.
Example
The following code shows how to use Math.Round(Decimal, Int32, MidpointRounding)
method.
using System;// w ww .j a va2 s. c o m
public class Example
{
public static void Main()
{
decimal[] values = { 2.125M, 2.135M, 2.145M, 3.125M, 3.135M, 3.145M };
foreach (double value in values)
Console.WriteLine("{0} --> {1}", value,
Math.Round(value, 2, MidpointRounding.AwayFromZero));
}
}
The code above generates the following result.