扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

c++四舍五入取整的方法是什么

扬州沐宇科技
2024-04-09 19:09:35
c++

在C++中,可以使用标准库中的round()函数来进行四舍五入取整操作。round()函数的功能是将浮点数四舍五入到最接近的整数值,并返回该整数值。

以下是round()函数的使用示例:

#include <iostream>
#include <cmath>

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

在上面的示例中,我们将浮点数3.7四舍五入取整,并输出结果4

扫码添加客服微信