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