【发布时间】:2018-03-15 09:05:16
【问题描述】:
#include <iostream>
using namespace std;
int main()
{
cout << -0.152454345 << " " << -0.7545 << endl;
cout << 0.15243 << " " << 0.9154878774 << endl;
}
输出:
-0.152454 -0.7545
0.15243 0.915488
我希望输出如下所示:
-0.152454 -0.754500
0.152430 0.915488
我的解决方案:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(6) << setw(9) << setfill(' ') << -0.152454345 << " ";
cout << fixed << setprecision(6) << setw(9) << setfill(' ') << -0.7545 << endl;
cout << fixed << setprecision(6) << setw(9) << setfill(' ') << 0.15243 << " ";
cout << fixed << setprecision(6) << setw(9) << setfill(' ') << 0.9154878774 << endl;
}
输出很好,但代码很糟糕。可以做什么? 这是我的代码https://ideone.com/6MKd31
【问题讨论】:
-
使用 stdio.h。 iostream 不利于格式化。
-
你可以为它写一个函数吗?
标签: c++ formatting cout