【发布时间】:2021-02-14 05:47:45
【问题描述】:
我不想重复使用 setprecision 并固定在 cout 上。那么有什么办法可以只设置一个并将其用于代码块中的所有 cout 吗?'
int main()
{
double num1 = 1.00000, num2 = 2.00005;
cout **<< setprecision(5) << fixed <<** num1 << endl; <-- I do not to repeatedly set this.
cout **<< setprecision(5) << fixed** << num2 << endl;
}
我在下面尝试过,但它不起作用
int main()
{
double num1 = 1.00000, num2 = 2.00005;
cout.precision(5); cout.fixed; cout.showpoint; <-- Does not work
cout << num1 << endl << num2 << endl;
}
欢迎提出任何建议。
【问题讨论】:
-
创建一个函数并传递你想要的 cout 作为参数