How is the round function used in C++?

In C++, the round function is used to round a floating point number to the nearest integer. Its usage is as follows:

#include <cmath>

double round(double x); // 返回一个最接近x的整数

float roundf(float x); // 返回一个最接近x的整数

long double roundl(long double x); // 返回一个最接近x的整数

For example:

#include <iostream>
#include <cmath>

int main() {
    double num = 4.6;
    double roundedNum = round(num);
    std::cout << "Rounded Number: " << roundedNum << std::endl;
    
    return 0;
}

The output result is:

Rounded Number: 5

In this example, the round function rounds 4.6 to the nearest whole number, which is 5.

bannerAds