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)A doubleprecision floatingpoint value is rounded to the nearest integer. Syntax: It has the following syntax: Parameters:
Return Type:
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 doubleprecision floatingpoint 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 doubleprecision floatingpoint 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:
Return Type
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 doubleprecision floatingpoint 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:
Return Type:
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:
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 floatingpoint number to the closest integer with the option to specify the rounding behavior for midpoint cases. Syntax: It has the following syntax: Parameters:
It's an integer value where:
Return Value:
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:
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:
mode
Return 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:
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 doubleprecision floatingpoint value to a predetermined number of fractional digits. Syntax: It has the following syntax: Parameters:
Return Value:
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:
Return Value:
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:
Next TopicMethodinfo.invoke() in C#
