#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << 123456789 << endl;
cout << std::setw(9) << std::setfill('0') << 123 << endl; // 填充9位字符
cout << std::setw(6) << std::setfill('0') << 123456789 << endl;
cout << "-------------------------------" << endl;
cout << std::setbase(8) << 255 << endl;
cout << std::setbase(10) << 255 << endl;
cout << std::setbase(16) << 255 << endl;
cout << 255 << endl; // 这里还是这里输出16进制
cout << "-------------------------------" << endl;
cout << setprecision(0) << 1.123456 << endl
<< setprecision(1) << 1.123456 << endl
<< setprecision(2) << 1.123456 << endl
<< setprecision(3) << 1.123456 << endl
<< setprecision(4) << 1.123456 << endl;
cout << std::setiosflags(std::ios::fixed) << std::setprecision(8) << 12.123456 << endl;
cout << 12.123456 << endl; // 这里还是八位小数
return 0;
}
输出如下:

本文介绍了使用C++标准库中的iostream进行格式化的输出方法,包括数字填充、基数转换、精度控制等。通过实例展示了如何设置输出宽度、填充字符、数字的基数以及浮点数的精度。


1408

被折叠的 条评论
为什么被折叠?



