Example 1: Math.Round(Double)
This method takes a float number as a parameter in the function and returns a rounded integer value. The basic syntax for this function is:
Now we will use this function in an example of C sharp. Use the system library to add functionalities of C sharp in the function. Inside the main program, we have taken two double values by describing two possible cases. The first double value is taken in which the fractional part, the position of numbers written after the decimal point “.”, contains the number that is less than the halfway between two consecutive integers. We will apply the round function to this number.
As the round function is the property of Mathematics and lies in the case of Maths, so it is accessed through the Math object.
Similarly, we introduced a decimal number whose fractional part is greater than half of the two consecutive integers in the second case. The same round() function is applied to that number too. Close the code and save it. Now we will execute the code saved with the .cs extension.
We need to use a specified compiler for each programming language in the Linux operating system. So for C sharp, we need to use the MCS compiler to compile the code. Then Mono assists the file’s execution with the extension of file.exe, making the file executable on the terminal.
On execution, you will see that the number we have taken for case 1 and case 2 have different values after an applied round function. Two terms that play a basic role in round function are the FLOOR and the CEILING value. The floor is the part of the decimal number before the “.”. The second one is the portion after the “.”. If the fractional part of the decimal number is less than half of the integers, as in case 1, then the floor number is obtained.
On the other hand, if the decimal part is greater as we have taken the number in case 2, then the ceiling part of the number is obtained as the number is greater, so the 12 integers are rounded to 13. Both the resultants are integer values.
Example 2: Math.Round (Double, Int32)
This is the function in which the float double value taken as input is rounded to a specified number in the fractional position. This specified number is provided by the user up to which extent he wants to round the decimal number. The syntax for the function is described as:
Here x is the double float value that is to be rounded off. Y is the number of digits infractions to round up to an extent. This type is the system.Int32. The return type of this function is an integer value that is the nearest value, with the decimal portion having digits according to the input number. The example will have a double value with a decimal value less than half of the integers, and the second one has numbers greater than half of the integers.
Similarly, the second function is also applied with 2 integer values.
On the execution of the code, you will see that the first value will be rounded off with the decimal value having 4 digits. And the second number is rounded to the 2 values.
Example 3: Math.Round (Decimal)
This round function takes a decimal value as input and applies the round function as a parameter.
This function works so that only a Floor value is the part of the number before the decimal point is obtained. This resultant value depends on the ceiling portion or the part after the decimal point. If that part contains a value greater than halfway, the floor value is incremented by 1. On the other hand, if the value is less than half of the integers, the floor value remains the same. The decimal part in both cases is removed, and only the integral part is obtained.
In the example, we again use two cases to demonstrate the working of a round function having a decimal value as the parameter.
On the execution of the code, you will see that both the values have the floor part of the decimal value, and the value after the “.” is removed. This round function type is mostly used in Mathematical calculations.
Example 4: Math.Round(Double, Int32, MidpointRounding)
This function is utilized to round off the decimal number provided by the number to some extent. As the name indicates, it is a midpoint rounding which means that the number is rounded off in the middle of the decimal number. The function takes three parameters as arguments. One is the input double value; the second one is the digit of int32 type up to which we need to round the number. And the third one is the value that specifies and helps the rounding of a number to an odd or an even number.
An array is declared. All the numbers will be rounded off at a time by being subjected to a single round function. Now let us consider an example from this perspective.
An array of 4 double floating values is declared. Each double value in the array will be applied with the math.round function.
The “2” digit shows that the resultant value will contain up to two numbers after the decimal point. The midpoint rounding contains an even property, so each number will be rounded to the number that must be even. Those that are already in even nature will remain the same.
You can see the resultant value; the first one will remain dame up to the 2nd point as it contains an even value: the second number has “3” and the 5 next, so it will be rounded as 3.14. Similarly, the third one will be the same, and the 4th will be rounded to 8 from 7.
The round function in C sharp is used to turn the current value of the decimal number up to a specified number given as input. There are different approaches used to round the value. All the input values must be a decimal number that generates the integer values. A round function is a property of the mathematics class; hence it is accessed through the object of math. This function contains different arguments, either double or decimal values; each time, the resultant value is an integer, either with or without floor value.