#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