固定字节数
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// 输出宽度10个字符,左对齐,不足补空格,输出3
cout << setw(10) << setfill(' ') << left << 3 << endl;
// 输出宽度14,右对齐,不足补0,输出10
cout << setw(14) << setfill('0') << right << 10 << endl;
}
小数精度
#include <iostream>
#include <fstream>
#include <iomanip>
//用setprecision(n)设置精度,其中n表示精确到小数点后n位
using namespace std;
void main()
{
double aa = 10;
cout<<" 12345.0普通输出为:"<<12345.0<<endl;//输出12345
cout<<fixed<<setprecision(8)<<" 10保留8位有效数字输出为:"<<aa<<endl;
//fixed:表示普通方式输出,不采用科学计数法。fixed也可以放在setprecision(n)后面
cout<<" 12345.0保留8位有效数字输出为:"<<12345.0<<endl;
cout<<fixed<<setprecision(2)<<" -123.456保留2位有效数字输出为:"<<-123.456<<endl;
cout << setprecision (4); // 精度
cout.setf(ios::fixed,ios::floatfield); // 定点格式
cout << "123.45678901保留4位有效数字输出为:"<< 123.45678901<<endl;
printf(" %2.4f保留2位有效数字输出为:%2.2f\n",11.1191,11.1191);
system("pause");
}
结果