## Math.Round Method() in C#In this article, you will learn about the ## 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 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(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 value is rounded to the nearest integer.
It has the following syntax:
- The double value specifies the parameter that the method accepts. Round this double-precision floating-point value to the nearest integer.
**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.
Let us take an example to illustrate the
The rounded value of 22.52 is: 23 The rounded value of 22.48 is: 22
This C# code explains how to round two double-precision floating-point values, num_1 and num_2, to the nearest integer using the ## 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 in the System namespace.Math class
It has the following syntax:
**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.
- The
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.*Round*
Let us take an example to illustrate the
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
This code provides an easy example of how to utilize the ## 3. Math.Round(decimal value)In C#, the
It has the following syntax:
**value:**The type of this parameter is. It represents the decimal number that wishes to be rounded to the nearest integer.*decimal*
- 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.
Let us take an example to illustrate the
Original value: 18.4627 Rounded to the nearest integer: 18
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 , 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.'r'## 4. Math.Round(decimal value, int decimals)In C#, the
It has the following syntax:
**decimal x:**This parameter denotes the preferred Rounding of the decimal number. It is the input value that has to be rounded.**int y:**Theparameter indicates the number of decimal places to which you wish to round the decimal number x. It determines the precision of the rounding operation.*int y*
Let us take an example to illustrate the
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
This C # code defines a class called ## 5. Math.Round(double value, MidpointRounding mode)In C#, the
It has the following syntax:
**value:**The value parameter represents thenumber that you wish to round. As the input value for the rounding process, it is a double of that type.*floating-point***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.
- This returned value represents the value rounded to the specified number of decimal places (digits) according to the specified rounding mode.
Let us take an example to illustrate the
Rounded to two decimal places using ToEven mode: 152.37 Rounded to two decimal places using AwayFromZero mode: 152.372
- The use of the
function with various rounding modes and precision levels is demonstrated in this C# code.*Round* - The number 152.37245 is used to initialize the double variable num_1.
- The
mode is used to round num_1 to two decimal places, resulting in*"MidpointRounding.ToEven"*, which is 152.37 because the Rounding is done to the nearest even integer.*roundedToEven* - As
mode always rounds midpoint values away from zero, roundedAwayFromZero is computed by rounding num_1 to three decimal places. The result is 152.373.*"MidpointRounding.AwayFromZero"* - 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.
It has the following syntax:
**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.and*"Bankers' rounding"*are other names.*"round to nearest, ties to even"***AwayFromZero:**This mode rounds away from zero. It consistently rounds midpoint values away from zero, regardless of the direction of the number.
- A
value is returned by the round method.*decimal* - The value rounded using the specified rounding mode is represented by this returned value.
Let us take an example to illustrate the
Original Values Rounded Values 3.475 3 3.575 4 3.185 3 4.45 4 4.345 4 4.875 5
- The
method with*Round*mode is used in this C# code to round double numbers.*"MidpointRounding.ToEven"* - An array of values with different double values is defined.
- The program outputs a header displaying
and*"Original Values"*to provide a clear output format.*"Rounded Values"* - 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
It has the following syntax:
**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.
- 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.
Let us take an example to illustrate the
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
Using two alternative rounding modes ( ), 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: MidpointRounding.AwayFromZero rounds towards the nearest even number, while ToEven rounds towards the nearest integer, away from zero.AwayFromZero## 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
It has the following syntax:
**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. 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.*digits*
- The
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*Round***,**which will be of type decimal.
Let us take an example to illustrate the
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
- 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:
and*ToEven*.*MidpointRounding.AwayFromZero* or*"Math.Round(q, decimals, MidpointRounding.ToEven)"*are the rounding methods applied to each decimal value q in the values array throughout the loop.*"Math.Round(q, decimals, MidpointRounding.AwayFromZero)"*- 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.
Next TopicMethodinfo.invoke() in C# |