# Math.Round Method() in C#

In this article, you will learn about the Math.Round() method in C# with their different methods and examples.

## What is the Math.Round() method?

In C#, a decimal value can be rounded to the closest integer or a predetermined number of fractional digits using Math.Round() method, which is part of the System namespace. As the method is static, you may call it directly from the Math class without first instantiating an object of the Math class.

This method is pivotal in financial calculations, mathematical operations, and scenarios requiring precision. Flexible rounding capabilities ensure accurate representation of numerical data and facilitate consistent handling of values across various computational tasks, contributing to robustness and reliability in software development.

## Types of methods:

There are several types of Math.Round() method in C#. Some of them are as follows:

1. Math.Round(double value)
2. Math.Round(double value, int digits)
3. Math.Round(decimal value)
4. Math.Round(decimal value, int decimals)
5. Math.Round(double value, MidpointRounding mode)
6. Math.Round(decimal value, MidpointRounding mode)
7. Math.Round(double value, int digits, MidpointRounding mode)
8. Math.Round(decimal value, int decimals, MidpointRounding mode)

### 1. Math.Round(double value)

A double-precision floating-point value is rounded to the nearest integer.

Syntax:

It has the following syntax:

Parameters:

• The double value specifies the parameter that the method accepts. Round this double-precision floating-point value to the nearest integer.

Return Type:

• double: It indicates the data type of the value the method returned. In this particular case, a double-precision floating-point number results from the method.

Example:

Let us take an example to illustrate the Math.Round(double value) method in C#.

Output:

```The rounded value of 22.52 is: 23
The rounded value of 22.48 is: 22
```

Explanation:

This C# code explains how to round two double-precision floating-point values, num_1 and num_2, to the nearest integer using the Math.Round() method. The Math.Round() method is called twice in the Main method, using the values num_1 and num_2. After that, the variables r1 and r2 are used to store the rounded values. Lastly, string interpolation displays the rounded numbers and the original values on the console.

### 2. Math.Round(double value, int digits)

In C#, a double-precision floating-point number can be rounded to a predetermined number of fractional digits using the Math.Round(double value, int digits) function. It is a member of the Math class in the System namespace.

Syntax:

It has the following syntax:

Parameters:

• value: A double is the type of this argument. It stands for the desired rounded number.
• digits: The type of this parameter is int. It specifies the number of fractional digits to round. It may be zero, negative, or positive. It shows how many decimal places to Round to if it is positive, how many digits to Round to the left of the decimal point if it is negative, and how to round to the nearest integer if it is zero.

Return Type

• The Round function returns a value of type double. Its returned value represents the rounded number. If no fractional part remains after rounding, it may be an integer; otherwise, it may be a double with the specified number of decimal places.

Example:

Let us take an example to illustrate the Math.Round(double value, int digits) method in C#.

Output:

```Rounded to the nearest integer: 4
Rounded to two decimal places: 4.24
Rounded to one decimal place: 4.2
Rounded to zero decimal places: 4
Rounded to three decimal places: 4.243
```

Explanation:

This code provides an easy example of how to utilize the Math.Round function in the C# round method to round a double-precision floating-point value to the nearest integer or a variety of decimal places.

### 3. Math.Round(decimal value)

In C#, the Math.Round(decimal value) function is used to round a decimal number to the closest integer. Using this approach, a decimal number with a precision of 128 bits is rounded to the closest integer value.

Syntax:

It has the following syntax:

Parameter:

• value: The type of this parameter is decimal. It represents the decimal number that wishes to be rounded to the nearest integer.

Return Type:

• The Math.Round(decimal value) function returns a decimal value. After rounding a decimal number to its nearest integer, this returned result indicates the rounded number, which is an integer.

Example:

Let us take an example to illustrate the Math.Round(decimal value) method in C#.

Output:

```Original value: 18.4627
Rounded to the nearest integer: 18
```

Explanation:

A decimal variable is initialized with the value 18.4627 using this C# code. After that, the value is rounded to the nearest integer using the Math.Round method, which does not require the number of decimal places to be specified. In 'r', the rounded value is stored. Finally, both the original value of value and the rounded value 'r' are printed to the console, providing insight into the rounding behaviour of the Math.Round method when applied to decimal numbers.

### 4. Math.Round(decimal value, int decimals)

In C#, the Math.Round(decimal value, int decimals) method represents a decimal number that can be rounded to a certain number of decimal places. An integer designating the number of decimal places to Round to and a decimal value are required. It returns a rounded decimal value, ensuring precision in mathematical operations.

Syntax:

It has the following syntax:

Parameters:

• decimal x: This parameter denotes the preferred Rounding of the decimal number. It is the input value that has to be rounded.
• int y: The int y parameter indicates the number of decimal places to which you wish to round the decimal number x. It determines the precision of the rounding operation.

Example:

Let us take an example to illustrate the Math.Round(decimal value, int decimals) method in C#.

Output:

```Rounded to the nearest integer: 22
Rounded to one decimal place: 22.3
Rounded to two decimal places: 22.35
Rounded to three decimal places: 22.345
```

Explanation:

This C # code defines a class called Demo, which has a Main function. Using the Math.Round method, it demonstrates how to round a decimal number of 22.3452m to the nearest integer and various decimal places (1, 2, and 3). The rounded numbers and the appropriate messages are written to the console. Overall, this code illustrates how to perform various rounding operations on decimal values using Math.Round in C#.

### 5. Math.Round(double value, MidpointRounding mode)

In C#, the Math.Round(double value, MidpointRounding mode) method is used to round a floating-point number to the closest integer with the option to specify the rounding behavior for midpoint cases.

Syntax:

It has the following syntax:

Parameters:

• value: The value parameter represents the floating-point number that you wish to round. As the input value for the rounding process, it is a double of that type.
• digits: This parameter sets the desired number of decimal places to round the value.

It's an integer value where:

• Positive numbers denote rounding to a certain number of decimal places to the right of the decimal point.
• Negative numbers denote rounding to the specified number of decimal places to the left of the decimal point.

Return Value:

• This returned value represents the value rounded to the specified number of decimal places (digits) according to the specified rounding mode.

Example:

Let us take an example to illustrate the Math.Round(double value, MidpointRounding mode) method in C#.

Output:

```Rounded to two decimal places using ToEven mode: 152.37
Rounded to two decimal places using AwayFromZero mode: 152.372
```

Explanation:

• The use of the Round function with various rounding modes and precision levels is demonstrated in this C# code.
• The number 152.37245 is used to initialize the double variable num_1.
• The "MidpointRounding.ToEven" mode is used to round num_1 to two decimal places, resulting in roundedToEven, which is 152.37 because the Rounding is done to the nearest even integer.
• As "MidpointRounding.AwayFromZero" mode always rounds midpoint values away from zero, roundedAwayFromZero is computed by rounding num_1 to three decimal places. The result is 152.373.
• After that, the program prints both rounded values, showcasing the impact of the rounding mode and precision level on the result.

### 6. Math.Round(decimal value, MidpointRounding mode)

A decimal number can be rounded to the nearest integer using this approach. If the value is midway between two integers, a parameter tells you how to round it.

Syntax:

It has the following syntax:

Parameters:

• value: The decimal number you wish to round is represented by this parameter.
• It is a decimal number used as the rounding method's input value.

mode

• ToEven: The nearest even value is rounded to in this mode. "Bankers' rounding" and "round to nearest, ties to even" are other names.
• AwayFromZero: This mode rounds away from zero. It consistently rounds midpoint values away from zero, regardless of the direction of the number.

Return Value:

• A decimal value is returned by the round method.
• The value rounded using the specified rounding mode is represented by this returned value.

Example:

Let us take an example to illustrate the Math.Round(decimal value, MidpointRounding mode) method in C#.

Output:

```Original Values	Rounded Values
3.475				3
3.575				4
3.185				3
4.45				4
4.345				4
4.875				5
```

Explanation:

• The Round method with "MidpointRounding.ToEven" mode is used in this C# code to round double numbers.
• An array of values with different double values is defined.
• The program outputs a header displaying "Original Values" and "Rounded Values" to provide a clear output format.
• Every value in the array is iterated using a foreach loop andRound is applied using the "ToEven" mode. Round is performed to round the value to the nearest integer.
• The original value and its rounded equivalent are displayed for each entry in the array.

### 7. Math.Round(double value, int digits, MidpointRounding mode)

In C#, the Math.Round method is utilized to specify the rounding mode when rounding a double-precision floating-point value to a predetermined number of fractional digits.

Syntax:

It has the following syntax:

Parameters:

• value: The value parameter represents the double-precision floating-point number you wish to round. You wish to use Rounding to modify this number.
• digits: An integer parameter that specifies how many fractional digits you wish to round the value to. If the digits are positive, the value will be rounded to the number of decimal places. If any of the digits are negative, the result will, based on the absolute value of the digits, be rounded to the nearest multiple of 10, 100, 1000, etc.

Return Value:

• Using the supplied rounding mode (mode), the Math.Round method yields a double-precision floating-point value rounded to the specified number of fractional digits (digits). The value supplied as the method's input is rounded to get the return value. It will be a double type.

Example:

Let us take an example to illustrate the Math.Round(double value, int digits, MidpointRounding mode) method in C#.

Output:

```Using MidpointRounding.ToEven mode for Rounding:
The original value is 3.283, and the Rounded value is: 3.28
The original value is 2.486, and the Rounded value is: 2.49
The original value is 2.246, and the Rounded value is: 2.25
The original value is: -2.356, and the Rounded value is: -2.36
The original value is: -4.327, and the Rounded value is: -4.33
The original value is: -4.565, and the Rounded value is: -4.57
Using MidpointRounding.AwayFromZero mode for Rounding:
The original value is 3.283, and the Rounded value is: 3.28
The original value is: 2.486, and the Rounded value is: 2.49
The original value is: 2.246, and the Rounded value is: 2.25
The original value is: -2.356, and the Rounded value is: -2.36
The original value is: -4.327, and the Rounded value is: -4.33
The original value is: -4.565, and the Rounded value is: -4.57
```

Explanation:

Using two alternative rounding modes (MidpointRounding.ToEven and MidpointRounding.AwayFromZero), this C# code demonstrates how to use the Math. Round method. It specifies variable digits set to 2, signifying rounding to two decimal places, and an array of sample double values. After that, the code rounds each value in the array using both rounding mechanisms until it reaches the desired number of digits. Printing the original and rounded values to the console occurs at each iteration. This method demonstrates how the rounding behaviour differs between the modes: ToEven rounds towards the nearest even number, while AwayFromZero rounds towards the nearest integer, away from zero.

### 8. Math.Round(decimal value, int decimals, MidpointRounding mode)

A method offered by the .NET framework for rounding decimal numbers to a specific number of digits using a chosen rounding mode is Math.Round(decimal value, int digits, MidpointRounding mode) in C#.

Syntax:

It has the following syntax:

Parameters:

• value: This parameter represents the decimal number that you wish to round. It's the numeric value that you wish to manipulate by Rounding.
• digits: The number of fractional digits to which you wish to round the value is indicated by an integer parameter called digits. If the digits are positive, a value will be rounded to the number of decimal places. The amount will be rounded to the nearest multiple of 10, 100, 1000, etc., based on the absolute value of the digits if they are negative.

Return Value:

• The Round method returns a decimal value rounded to the specified number of fractional digits (digits) using the provided rounding mode (mode). Rounding the value provided as an input to the method produces the return value, which will be of type decimal.

Example:

Let us take an example to illustrate the Math.Round(decimal value, int decimals, MidpointRounding mode) method in C#.

Output:

```Using MidpointRounding.ToEven mode for Rounding:
The original value is 2.356, and the Rounded value is: 2.36
The original value is: 4.386, and the Rounded value is: 4.39
The original value is 3.348, and the Rounded value is 3.35
The original value is: -1.264, and the Rounded value is: -1.26
The original value is: -3.285, and the Rounded value is: -3.28
The original value is: -3.385, and the Rounded value is: -3.38
Using MidpointRounding.AwayFromZero mode for Rounding:
The original value is: 2.356, and the Rounded value is: 2.36
The Original value is: 4.386, and the Rounded value is: 4.39
The original value is: 3.348, and the Rounded value is: 3.35
The original value is: -1.264, and the Rounded value is: -1.26
The original value is: -3.285, and the Rounded value is: -3.29
The original value is: -3.385, and the Rounded value is: -3.39
```

Explanation:

• In this example, two different rounding options are demonstrated that uses decimal numbers to demonstrate how to use the Round method.
• An integer variable called decimals is set to 2, signifying rounding to two decimal places, and an array named values is initialized with decimal values by the Main method. After that, the code enters a loop demonstrating two distinct Rounding forms: ToEven and MidpointRounding.AwayFromZero.
• "Math.Round(q, decimals, MidpointRounding.ToEven)" or "Math.Round(q, decimals, MidpointRounding.AwayFromZero)" are the rounding methods applied to each decimal value q in the values array throughout the loop.
• After that, the rounded values are printed to the console alongside their original elements, allowing developers to observe the impact of each rounding mode on the values.