c++ 除法四舍五入
时间: 2023-10-18 08:26:33 浏览: 162
C++ 四舍五入函数
要在C++中实现除法的四舍五入,可以使用以下代码:
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double num1, num2;
cin >> num1 >> num2;
double result = num1 / num2;
cout << fixed << setprecision(2) << result << endl;
return 0;
}
在这个代码中,我们首先输入两个数字num1和num2,然后将num1除以num2得到结果result。接下来,使用fixed和setprecision(2)来设置输出精度为两位小数。最后,将result输出到屏幕上。
这样,就能够实现除法的四舍五入,并输出结果保留两位小数。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [【C++】C++如何进行四舍五入](https://blog.csdn.net/weixin_54438368/article/details/123631972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [c++的四舍五入](https://blog.csdn.net/weixin_50910324/article/details/120120071)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文