C round() Function

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In C programming language, there are many mathematical functions used for fast calculations, and also they are very simple to use. One of these functions is the C round() function and it is present inside the <math.h> module of the C Library. The basic intuition of using the C round() functions is that it easily converts a number to its nearest integer value (which may be small or large).

What is C round() Function?

The C round() function is one of the mathematical functions which is found in <math.h> module of the C library. The round() function in C returns the nearest integer value (rounded value) of the given float, integer, or double number based on the decimal part of the number. If the decimal value of the number provided is less than or equal to 0.5, then the value returned will be the integer smaller than the number provided in the argument, and if the decimal value of the number provided is greater than 0.5, then the value returned will be the integer greater than the number provided in the argument. The integer returned by the round() function will be proper and it will not contain any decimal part.

Syntax of C round() Function

The syntax of the round function is very simple, it just accepts one argument which can be float, int, or double, and it returns the integer by rounding it off to the accurate value. Its generic syntax is as follows

Here the num variable can be of float, or double type and the roundedValue will be an integer returned by the round() function and the roundedValue will not contain any decimal part.

Parameters of C round() Function

The C round function accepts only one value which can be passed as a parameter. The parameter of the C round function is a real number and it can be of the form float, integer, double or long double. If the user provides a variable of any other data type, then a compiler error will occur in the code because this function can accept only numbers as an argument.

Return Value of C round() Function

The round() function in C returns the nearest integer value (rounded off value) of the number provided as an argument which can be of type float, integer, or double number provided as an argument. If the decimal value of the number provided is less than or equal to 0.5, then the value returned will be the integer smaller than the number provided in the argument, and if the decimal value of the number provided is greater than 0.5, then the value returned will be the integer greater than the number provided in the argument.

How does the round() Function in C work?

The round() function in C works in a very easy and simple way. The first step we have to follow is that we have to include the <math.h> library in C. Then we are good to start with the round() function. The round function accepts an argument that can be of any type from int, float, double or long double. It returns the closest integer value to that number. In the case of negative numbers, it will first convert the number into positive, and then it will apply the above logic to find the nearest integer to that number, and finally, it will again convert that integer to a negative integer and return a negative integer.

Uses of round() Function in C

The round function is one of the most used mathematical functions in the field of algebra, geometry, and many other areas of mathematics. It is mainly used to show the approximation of the decimal values, for example, finding the approximate value of the temperature, finding the estimated bank balance, the rating of movies or songs, etc... As it finds the nearest integer of the given number, it is also used widely in computational problems, Statistics, Machine Learning, Artificial Intelligence, etc...

C round() Function Examples

The C round() function can take the int, float, and double value as an argument and returns the nearest integer to that value after rounding it off. Below is an example of all types of arguments.

Example 1

Output

In the above case, the argument passed is a float number. The output, in this case, will be a smaller integer than this number because the decimal value is 0.014, which is less than 0.5, and as we have discussed above that if the decimal value will be less than or equal to 0.5, the round() function will return the smaller integer than this number. So the output, in this case, will be 7` because it is a smaller integer than the given float number.

Example 2

Output

In the above case, the argument passed is a float number. The output, in this case, will be a smaller integer than this number because the decimal value is 0.4, which is less than 0.5, and as we have discussed above that if the decimal value will be less than or equal to 0.5, the round() function will return the smaller integer than this number. So the output, in this case, will be 2 because it is a smaller integer than the given float number.

Example 3

Output

In the above case, the argument passed is a float number. The output, in this case, will be the greater integer than this number because the decimal value is 0.65, which is greater than 0.5, and as we have discussed above that if the decimal value will be greater than or equal to 0.5, the round() function will return the greater integer than this number with the precession of 0 because the value returned by the round() function does not contain any decimal value. So the output, in this case, will be 4 because it is a greater integer than the given float number.

Example 4

Output

In the above case, the argument passed is a double number. The output, in this case, will be a smaller integer than this number because the decimal value is 0.2325467 which is less than 0.5, and as we have discussed above that if the decimal value will be less than or equal to 0.5, the round() function will return the smaller integer than this number. So the output, in this case, will be 8 because it is a smaller integer than the given float number.

Example 5

Output

In the above case, the argument passed is a double number. The output, in this case, will be the greater integer than this number because the decimal value is 0.7390878 which is greater than 0.5, although as we have discussed above that if the decimal value will be greater than or equal to 0.5, the round() function will return the greater integer than this number with the precision of 0 because the value returned by the round() function does not contain any decimal value, and as it is the negative value case, it will return the opposite, this means it first converts the negative value to the positive value and then returns the integer value. So the output, in this case, will be -6 because it is a greater integer than the given float number.

Conclusion

In this quick tutorial, we have learned about the C round() function which is widely used in mathematics for fast and efficient calculation, and we have discussed the following

  • Learned about the syntax of the C round() function.
  • Learned about the parameters and arguments which are accepted by the C round() function.
  • The return value of the C round() function i.e. in which form the value is returned by this function.
  • The working of the C round() function, for both positive and negative numbers.
  • Finally, the examples of the C round() function and covering all the possible cases.