Round() in C++

round is utilized to adjust the given digit which can be in float or twofold. It returns the closest basic worth to gave boundary in round work, with midway cases adjusted away from nothing. Rather than round(), std::round() can likewise be utilized .

The round() work in C++ is utilized to adjust the twofold, float or long twofold worth passed to it as a boundary to the closest necessary worth. The header record used to utilize the round() work in a c++ program is <cmath> or <tgmath>.

Potential Applications

Dealing with the crisscross among portions and decimal : One utilization of adjusting numbers is abbreviate all the three’s to one side of the decimal point in changing over 1/3 to decimal. More often than not, we will utilize the adjusted numbers 0.33 or 0.333 when we really want to work with 1/3 in decimal. We generally work with only a few digits to one side of the decimal moment that there is no accurate identical to the part in decimal.
Changing duplicated result : There will be distinction between augmentation of 25, 75 and 0.25, 0.75 we get 0.875 .We began with 2 digits to one side of the decimal point and wound up with 4. Commonly we will simply gather together the outcome to 0.19 .

Quick estimation : Suppose needing quick computation we take approx esteem and afterward work out closest reply. For instance, we find a solution 298.78 later any computation and by adjusting we find an outright solution of 300.
Getting gauge : Sometimes you need to adjust whole numbers rather than decimal numbers. Generally you are keen on adjusting to the closest different of 10, 100, 1, 000 or million. For instance, in 2006 the not really settled that the number of inhabitants in New York City was 8, 214, 426. That number is difficult to recollect and assuming we say the number of inhabitants in New York City is 8 million it is a decent gauge since it doesn’t have any genuine effect what the specific number is.

Parameters: x, value to be rounded double round (double x); float round (float x); long double round (long double x); double round (T x); // additional overloads for integral types Returns: The value of x rounded to the nearest integral (as a floating-point value).

// C++ code to demonstrate the
// use of round() function
#include <cmath>
#include <iostream>
using namespace std;
 
// Driver program
int main()
{
    // initializing value
    double x = 12.5, y = 13.3, z = 14.8;
 
    // Displaying the nearest values
    // of x, y and z
    cout << "Nearest value of x :" << round(x) << "\n";
    cout << "Nearest value of y :" << round(y) << "\n";
    cout << "Nearest value of z :" << round(z) << "\n";
 
    // For lround
    cout << "lround(-0.0) = " << lround(-0.0) << "\n";
    cout << "lround(2.3) = " << lround(2.3) << "\n";
    cout << "lround(2.5) = " << lround(2.5) << "\n";
    cout << "lround(2.7) = " << lround(2.7) << "\n";
    cout << "lround(-2.3) = " << lround(-2.3) << "\n";
    cout << "lround(-2.5) = " << lround(-2.5) << "\n";
    cout << "lround(-2.7) = " << lround(-2.7) << "\n";
 
    // For llround
    cout << "llround(-0.01234) = " << llround(-0.01234) << "\n";
    cout << "llround(2.3563) = " << llround(2.3563) << "\n";
    cout << "llround(2.555) = " << llround(2.555) << "\n";
    cout << "llround(2.7896) = " << llround(2.7896) << "\n";
    cout << "llround(-2.323) = " << llround(-2.323) << "\n";
    cout << "llround(-2.5258) = " << llround(-2.5258) << "\n";
    cout << "llround(-2.71236) = " << llround(-2.71236) << "\n";
 
    return 0;
}

Output:

Nearest value of x :13
Nearest value of y :13
Nearest value of z :15
lround(-0.0) = 0
lround(2.3) = 2
lround(2.5) = 3
lround(2.7) = 3
lround(-2.3) = -2
lround(-2.5) = -3
lround(-2.7) = -3
llround(-0.01234) = 0
llround(2.3563) = 2
llround(2.555) = 3
llround(2.7896) = 3
llround(-2.323) = -2
llround(-2.5258) = -3
llround(-2.71236) = -3

Here, in the above program we have just calculated the nearest integral value of given float or double value.
which has been calculated accurately.

This article is contributed by Himanshu Ranjan. Assuming you like GeeksforGeeks and might want to contribute, you can likewise compose an article utilizing contribute.geeksforgeeks.org or mail your article to [email protected] See your article showing up on the GeeksforGeeks fundamental page and help different Geeks.

Kindly record bits of feedback in the event that you find anything wrong, or you need to share more data about the theme examined previously.

// C+++ code for above explanation
#include <cmath>
#include <iostream>
using namespace std;
 
// Driver program
int main()
{
    // Initializing values for int type
    long int a1 = 25, b1 = 30;
 
    // Initializing values for double type
    double a2 = .25, b2 = .30;
    long int ans_1 = (a1 * b1);
    double ans_2 = (a2 * b2);
 
    // Rounded result for both
    cout << "From first multiplication :" << round(ans_1) << "\n";
    cout << "From second multiplication :" << round(ans_2) << "\n";
    return 0;
}

Output:

    From first multiplication :750
    From second multiplication :0

round() Prototypes

The prototypes of the round() function as defined in the cmath header file are:

double round(double num);

float round(float num);

long double round(long double num);

// for integral type
double round(T num);

Example 1: C++ round()

#include <iostream>
#include <cmath>
using namespace std;

int main() {

  double num, result;
num = 11.16; result = round(num);


  cout << "round(" << num << ") = " << result << endl;
num = 13.87; result = round(num);


  cout << "round(" << num << ") = " << result << endl;
num = 50.5; result = round(num);


  cout << "round(" << num << ") = " << result;
      
  return 0;
}

Output

round(11.16) = 11
round(13.87) = 14
round(50.5) = 51

Example 2: C++ round() for Negative Numbers

#include <iostream>
#include <cmath>
using namespace std;

int main() {

  double num, result;
num = -11.16; result = round(num);


  cout << "round(" << num << ") = " << result << endl;
num = -13.87; result = round(num);


  cout << "round(" << num << ") = " << result << endl;
num = -50.5; result = round(num);


  cout << "round(" << num << ") = " << result;
      
  return 0;
}

Output

round(-11.16) = -11
round(-13.87) = -14
round(-50.5) = -51

Example 3: C++ round() for Integral Types

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  double result;
int num = 15; result = round(num);


  cout << "round(" << num << ") = " << result;

  return 0;
}

Output

round(15) = 15

For integral values, applying the round() function returns the same value as the input. So it is not commonly used for integral values in practice.

Leave a Reply

Your email address will not be published.