setw() setfill()
cout<<setw(5)<<setfill('0')<<a<<b;
##输出5位,右对齐,不足补0
1、setw(int n)
只是对直接跟在<<后的输出数据起作用,而在之后的<<需要在之前再一次使用setw
;
2、n是在输出时分配了n个字符的输出宽度,然后默认的是在n个字符宽度中左对齐输出;
3、使用setfill(char x)
使用指定字符变量x
来填充空下的空格;
#include<bits/stdc++.h>
using namespace std;
int main()
{
double a=11;
double b=123;
cout<<setw(5)<<setfill('0')<<a<<" "<<b;
cout<<endl;
cout<<setw(5)<<setfill('0')<<b;
return 0;
}
运行结果: