#include <iostream>
#include <cmath> // 引入cmath库,用于使用round函数
using namespace std;
int main() {
double num = 3.65; // 定义一个需要四舍五入的浮点数
double rounded_num = round(num); // 使用round函数进行四舍五入
cout << "原始数值: " << num << endl;
cout << "四舍五入后的数值: " << rounded_num << endl;
return 0;
}
#include <cmath> 是为了使用 round() 函数,该函数可以对浮点数进行四舍五入。double num = 3.65; 定义了一个双精度浮点数 num,其值为 3.65。double rounded_num = round(num); 使用 round() 函数对 num 进行四舍五入,并将结果存储在 rounded_num 中。cout 输出原始数值和四舍五入后的数值。如果你需要处理更复杂的四舍五入逻辑(例如指定小数位数),可以进一步修改代码。
上一篇:c++ 线程
下一篇:sort函数c++用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站