In this guide, you will explore the Math.Round function in C# along with its various overloads and illustrative samples.
What is the Math.Round method?
In C#, you can round a decimal value to the nearest integer or a specific number of decimal places by utilizing the Math.Round method, which belongs to the System namespace. Since this method is static, there is no need to create an instance of the Math class before calling it.
This approach is crucial for financial computations, mathematical procedures, and situations that demand accuracy. Adaptable rounding functionalities guarantee precise portrayal of numeric information and support uniform management of values throughout different computational operations, enhancing resilience and dependability in software creation.
Types of methods:
There are several types of Math.Round method in C#. Some of them are as follows:
- Math.Round(double value)
- Math.Round(double value, int digits)
- Math.Round(decimal value)
- Math.Round(decimal value, int decimals)
- Math.Round(double value, MidpointRounding mode)
- Math.Round(decimal value, MidpointRounding mode)
- Math.Round(double value, int digits, MidpointRounding mode)
- Math.Round(decimal value, int decimals, MidpointRounding mode)
1. Math.Round(double value)
A double-precision floating-point number is approximated to the closest whole number.
Syntax:
It has the following syntax:
public static double Round(double value);
- The parameter is a double value that the method can take, and it needs to be rounded to the nearest integer when utilized.
Return Type:
The
- double specifies the data type of the returned value by the method. In this instance, the method yields a double-precision floating-point number.
Example:
Let's consider a scenario to demonstrate the application of the Math.Round(double value) function in the C# programming language.
using System;
class Demo
{
static void Main(string[] args)
{
double num_1 = 22.52;
double num_2 = 22.48;
//Rounding to the nearest integer
double r1 = Math.Round(num_1);
double r2 = Math.Round(num_2);
Console.WriteLine($"The rounded value of {num_1} is: {r1}");
Console.WriteLine($"The rounded value of {num_2} is: {r2}");
}
}
Output:
The rounded value of 22.52 is: 23
The rounded value of 22.48 is: 22
Explanation:
This C# script demonstrates the process of rounding two double-precision floating-point numbers, num1 and num2, to the closest integer by utilizing the Math.Round method. Within the Main method, Math.Round is invoked twice with the input values num1 and num2. The resulting rounded values are stored in variables r1 and r2. The final step involves showcasing both the rounded and original numbers through string interpolation on the console.
2. Math.Round(double value, int digits)
In C#, you can round a double-precision floating-point number to a specific number of decimal places by employing the Math.Round(double value, int digits) method. This method is part of the Math class within the System namespace.
Syntax:
It has the following syntax:
public static do:uble Round(double value, int digits);
Parameters:
- value: This argument is of type double and represents the specific rounded number that is desired.
- digits: The data type for this parameter is int. It indicates the count of fractional digits for rounding. It can have a value of zero, negative, or positive. This parameter determines the number of decimal positions to round to for positive values, the number of digits to round to the left of the decimal point for negative values, and the method to round to the nearest integer for zero values.
The return type of the Round function is a double. The value it returns signifies the rounded number, which could be an integer if there are no remaining decimal parts after rounding or a double with the designated decimal places if there are fractional parts.
Example:
Let's consider an example to demonstrate the application of the Math.Round(double value, int digits) method in the C# programming language.
using System;
class Demo
{
static void Main()
{
double number = 4.24263;
//Round to the nearest integer
double r1 = Math.Round(number);
Console.WriteLine($"Rounded to the nearest integer: {r1}");
//Round to two decimal places
double r2 = Math.Round(number, 2);
Console.WriteLine($"Rounded to two decimal places: {r2}");
//Round to just one decimal place
double r3 = Math.Round(number, 1);
Console.WriteLine($"Rounded to one decimal place: {r3}");
//Round to zero decimal places
double r4 = Math.Round(number, 0);
Console.WriteLine($"Rounded to zero decimal places: {r4}");
//Round to three decimal places
double r5 = Math.Round(number, 3);
Console.WriteLine($"Rounded to three decimal places: {r5}");
}
}
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 offers a simple illustration of applying the Math.Round function within the C# round method to round a double-precision floating-point number to the nearest whole number or different decimal positions.
3. Math.Round(decimal value)
In C#, the Math.Round(decimal value) method is employed to round a decimal number to the nearest whole number. By utilizing this technique, a decimal number with a precision of 128 bits is rounded to the nearest integer value.
Syntax:
It has the following syntax:
public static decimal Round(decimal value);
Value of:
- : This parameter holds a decimal value, indicating the specific decimal number that needs to be rounded to the closest integer.
Return Type:
The function Math.Round(decimal value) returns a decimal value representing the rounded integer value of the input decimal number. The result signifies the integer value after rounding the decimal number to the nearest whole number.
Example:
Let's consider a scenario to demonstrate the application of the Math.Round(decimal value) function in C#.
using System;
class Demo
{
static void Main()
{
decimal value = 18.4627m;
//Rounding to the nearest integer
decimal r = Math.Round(value);
Console.WriteLine("Original value: " + value);
Console.WriteLine("Rounded to the nearest integer: " + r);
}
}
Output:
Original value: 18.4627
Rounded to the nearest integer: 18
Explanation:
A decimal variable is set to 18.4627 in the given C# code. Following this, the value undergoes rounding to the nearest whole number through the application of the Math.Round function, which operates without the need to indicate the decimal precision. The rounded value is stored in 'r'. Subsequently, both the initial value of the variable and the rounded value 'r' are displayed on the console, offering a glimpse into how the Math.Round method functions in rounding decimal values.
4. Math.Round(decimal value, int decimals)
In C#, the Math.Round(decimal value, int decimals) function is responsible for rounding a decimal number to a specified number of decimal places. It necessitates an integer indicating the desired decimal precision and a decimal value for input. The method outputs a rounded decimal value, maintaining accuracy in mathematical computations.
Syntax:
It has the following syntax:
public static decimal Round(decimal x, int y)
- decimal x: This argument represents the desired rounding of the decimal value. It serves as the value that needs to undergo rounding.
- int y: The integer y parameter specifies the quantity of decimal points to round the decimal x value to. This parameter sets the accuracy level of the rounding process.
Example:
Let's consider a scenario to demonstrate the application of the Math.Round(decimal value, int decimals) function in the C# programming language.
using System;
class Demo
{
static void Main()
{
decimal value = 22.3452m;
//Rounding to the nearest integer
decimal r = Math.Round(value);
Console.WriteLine("Rounded to nearest integer: " + r);
//Rounding to just one decimal place
r = Math.Round(value, 1);
Console.WriteLine("Rounded to one decimal place: " + r);
//Rounding to two decimal places
r = Math.Round(value, 2);
Console.WriteLine("Rounded to two decimal places: " + r);
//Rounding to three decimal places
r = Math.Round(value, 3);
Console.WriteLine("Rounded to three decimal places: " + r);
}
}
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# script introduces a class named Example containing a Main method. It showcases the utilization of the Math.Round function to round a decimal value, 22.3452m, to the nearest whole number and different decimal positions such as 1, 2, and 3. The resulting rounded numbers and corresponding messages are displayed on the console. This code snippet serves as a demonstration of conducting diverse rounding tasks on decimal figures by leveraging the Math.Round functionality in C#.
5. Math.Round(double value, MidpointRounding mode)
In C#, the Math.Round(double value, MidpointRounding mode) function is employed to approximate a decimal number to the nearest whole number while allowing customization of the rounding strategy for midpoint scenarios.
Syntax:
It has the following syntax:
public static double Round(double value, int digits, MidpointRounding mode);
Parameters:
The
- value parameter denotes the floating-point number intended for rounding. It is a double data type serving as the input for the rounding operation.
- digits: This parameter specifies the number of decimal places to which the value should be rounded.
Positive values represent the act of rounding to a specific number of decimal positions to the right of the decimal point. Conversely, negative values indicate rounding to the designated number of decimal places to the left of the decimal point.
Return Value:
The value that is returned signifies the value that has been rounded to the designated number of decimal places based on the indicated rounding mode.
Example:
Let's consider a scenario to demonstrate the application of the Math.Round(double value, MidpointRounding mode) function in the C# programming language.
using System;
class Demo
{
static void Main()
{
double num_1 = 152.37245;
//Rounding with "MidpointRounding.ToEven mode" to two decimal places.
double roundedToEven = Math.Round(num_1, 2, MidpointRounding.ToEven);
Console.WriteLine($"Rounded to two decimal places using ToEven mode: {roundedToEven}");
//Rounding with "MidpointRounding.AwayFromZero mode" to three decimal places.
double roundedAwayFromZero = Math.Round(num_1, 3, MidpointRounding.AwayFromZero);
Console.WriteLine($"Rounded to two decimal places using AwayFromZero mode: {roundedAwayFromZero}");
}
}
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 value can be approximated to the closest whole number with this method. When the number falls exactly between two integers, a specific parameter guides the rounding process.
Syntax:
It has the following syntax:
public static decimal Round(decimal value, MidpointRounding mode);
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.
- 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:
The round method returns a decimal value upon completion.
This returned value represents the result of rounding the input value based on the specified rounding mode.
Example:
Let's consider an example to demonstrate the application of the Math.Round(decimal value, MidpointRounding mode) method in the C# programming language.
using System;
class Demo
{
static void Main()
{
double[] values = { 3.475, 3.575, 3.185, 4.45, 4.345, 4.875 };
Console.WriteLine("Original Values\tRounded Values");
foreach (double i in values)
{
// Use the "MidpointRounding.ToEven" mode to round the value.
double r = Math.Round(i, MidpointRounding.ToEven);
Console.WriteLine($"{i}\t\t\t\t{r}");
}
}
}
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 and Round 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 function is employed to define the rounding mode for rounding a double-precision floating-point number to a specific number of decimal places.
Syntax:
It has the following syntax:
double Math.Round(double value, int digits, MidpointRounding mode)
Parameters:
- value: This parameter signifies the double-precision floating-point number that you aim to round. Rounding is applied to adjust this number.
- digits: A parameter represented by an integer that determines the number of decimal places to which the value should be rounded. Positive digits round the value to decimal places, while negative digits round the value to the nearest multiple of 10, 100, 1000, and so on, based on the absolute value of the digits.
Return Outcome:
By applying the designated rounding mode (mode), the Math.Round function produces a double-precision floating-point result rounded to the designated count of decimal places (digits). The provided input to the function undergoes rounding to generate the output, which will be of double data type.
Example:
Let's consider a scenario to demonstrate the application of the Math.Round(double value, int digits, MidpointRounding mode) function in C#.
using System;
class Demo
{
static void Main()
{
// Defining some sample values
double[] values = { 3.283, 2.486, 2.246, -2.356, -4.327, -4.565 };
int digits = 2;
// Demonstrating Rounding with different modes
Console.WriteLine("Using MidpointRounding.ToEven mode for rounding:");
foreach (double q in values)
{
double r = Math.Round(q, digits, MidpointRounding.ToEven);
Console.WriteLine($"Original value is: {q}, Rounded value is: {r}");
}
Console.WriteLine("\nUsing MidpointRounding.AwayFromZero mode for rounding:");
foreach (double q in values)
{
double r = Math.Round(q, digits, MidpointRounding.AwayFromZero);
Console.WriteLine($"Original value is: {q}, Rounded value is: {r}");
}
}
}
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:
Utilizing two alternative rounding modes (MidpointRounding.ToEven and MidpointRounding.AwayFromZero), the following C# script showcases the application of the Math.Round method. It defines a variable "digits" set to 2, indicating rounding to two decimal places, along with an array containing sample double values. Subsequently, the script proceeds to round each value in the array using both rounding strategies until reaching the specified number of digits. At each iteration, the original and rounded values are output to the console. This illustration effectively highlights the distinction in rounding behavior between the two modes: ToEven rounds to the nearest even number, while AwayFromZero rounds to the closest integer away from zero.
8. Math.Round(decimal value, int decimals, MidpointRounding mode)
A function provided by the .NET framework to round decimal values to a specified number of digits with a selected rounding method is Math.Round(decimal value, int digits, MidpointRounding mode) in C#.
Syntax:
It has the following syntax:
decimal Math.Round(decimal value, int digits, MidpointRounding mode)
Parameters:
- value: This parameter signifies the decimal number that you want to round. It's the numerical value that you want to adjust through the Rounding process.
- digits: An integer parameter named digits specifies the count of decimal places to round the value to. When digits are positive, the value will be rounded to the specified number of decimal places. In the case of negative digits, the value will be rounded to the nearest multiple of 10, 100, 1000, and so on, determined by the absolute value of digits.
Return Output:
- The Round function delivers a decimal result that is rounded to the designated number of decimal places using the specified rounding mode. By rounding the input value, the method generates an output of decimal data type.
Example:
Let's consider a scenario to demonstrate the application of the Math.Round(decimal value, int decimals, MidpointRounding mode) function in C#.
using System;
class Demo
{
static void Main()
{
// Defining some sample decimal values
decimal[] values = { 2.356m, 4.386m, 3.348m, -1.264m, -3.285m, -3.385m };
int decimals = 2;
// Demonstrating Rounding with different modes
Console.WriteLine("Using MidpointRounding.ToEven mode for rounding:");
foreach (decimal q in values)
{
decimal r = Math.Round(q, decimals, MidpointRounding.ToEven);
Console.WriteLine($"Original value is: {q}, Rounded value is: {r}");
}
Console.WriteLine("\nUsing MidpointRounding.AwayFromZero mode for rounding:");
foreach (decimal q in values)
{
decimal r = Math.Round(q, decimals, MidpointRounding.AwayFromZero);
Console.WriteLine($"Original value is: {q}, Rounded value is: {r}");
}
}
}
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.