如何在 C++ 中保留两位小数输出
在 C++ 中保留两位小数输出可以通过使用 fixed 和 setprecision() 函数。
使用方法:
#include#include using namespace std;int main() { double number = 123.4567; // 保留两位小数 cout 详细解释:
立即学习“tps://pan.quark.cn/s/6e7abc4abb9f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">C++免费学习笔记(深入)”;
- fixed:告诉 C++ 将浮点数表示为固定小数点。
- setprecision(2):设置小数位数为 2。
通过结合使用 fixed 和 setprecision(),可以控制浮点数的输出格式,并保留指定的小数位数。
示例输出:
123.45

