# C# Math - Round() Method

The C# Round() method is used to round a value to the nearest integer or to the specified number of fractional digits. The method can be overloaded and it can take decimal and double arguments. The method can also be used to specify the number of fractional digits after rounding and mode of rounding.

### Syntax

```public static decimal Round (decimal value, int digits, MidpointRounding mode);
public static double Round (double value, int digits, MidpointRounding mode);

public static double Round (double value, MidpointRounding mode);
public static decimal Round (decimal value, MidpointRounding mode);

public static double Round (double value, int digits);
public static decimal Round (decimal value, int digits);

public static double Round (double value);
public static decimal Round (decimal value);
```

### Parameters

 `Value` Specify the value to be Rounded. `digits` Specify the number of fractional digits after rounding. `MidpointRounding mode` Specify the mid point rounding mode. All MidpointRounding modes are as follows: AwayFromZero: rounded toward the nearest number that is away from zero. ToEven: rounded toward the nearest even number. ToNegativeInfinity: rounded toward the negative infinity. ToPositiveInfinity: rounded toward the positive infinity. ToZero: rounded toward the zero.

### Return Value

Returns a value to the nearest integer or to the specified number of fractional digits. If the value is midway between two integers, the mode determines the result.

### Exception

• Throws ArgumentException, if mode is not a valid value of MidpointRounding.
• Throws OverflowException, if the result is outside the range of a Decimal or Double.

### Example:

In the example below, Round() method is used to round a value to the nearest integer and to a precision of 1 decimal point.

```using System;

class MyProgram {
static void Main(){
Double[] values = { 12.49, 12.50, 12.51, 13.49, 13.50, 13.51};
Console.WriteLine(
"{0,-7} {1,-7} {2,-15}", "Value",
"Default", "1 fraction digit");
foreach (var value in values)
Console.WriteLine(
"{0,-7:R} {1,-7} {2,-15}",
value, Math.Round(value),
Math.Round(value, 1));
}
}
```

The output of the above code will be:

```Value   Default 1 fraction digit
12.49   12      12.5
12.5    12      12.5
12.51   13      12.5
13.49   13      13.5
13.5    14      13.5
13.51   14      13.5
```

### Example:

In the example below, Round() method is used to round a value using different mid point rounding mode.

```using System;

class MyProgram {
static void Main(){
Double[] values = { 12.49, 12.50, 12.51, 13.49, 13.50, 13.51};
Console.WriteLine(
"{0,-7} {1,-7} {2,-7} {3,-7} {4,-20}",
"Value", "Default", "ToEven","ToZero",
"1 fraction digit and ToZero");
foreach (var value in values)
Console.WriteLine(
"{0,-7:R} {1,-7} {2,-7} {3,-7} {4,-20}",
value, Math.Round(value),
Math.Round(value, MidpointRounding.ToEven),
Math.Round(value, MidpointRounding.ToZero),
Math.Round(value, 1, MidpointRounding.ToZero));
}
}
```

The output of the above code will be:

```Value   Default ToEven  ToZero  1 fraction digit and ToZero
12.49   12      12      12      12.4
12.5    12      12      12      12.5
12.51   13      13      12      12.5
13.49   13      13      13      13.4
13.5    14      14      13      13.5
13.51   14      14      13      13.5
```

❮ C# Math Methods

5