1)头文件:
#include <iomanip>//cin、cout操纵运算子
dec 设置整数为十进制
hex 设置整数为十六进制
oct 设置整数为八进制
setbase(n) 设置整数为n进制(n=8,10,16)
setfill(n) 设置字符填充,c可以是字符常或字符变量
setprecision(n) 设置浮点数的有效数字为n位
setw(n) 设置字段宽度为n位
setiosflags(ios::fixed) 设置浮点数以固定的小数位数显示
setiosflags(ios::scientific) 设置浮点数以科学计数法表示
setiosflags(ios::left) 输出左对齐
setiosflags(ios::right) 输出右对齐
setiosflags(ios::skipws) 忽略前导空格
setiosflags(ios::uppercase) 在以科学计数法输出E与十六进制 输出X以大写输出,否则小写。
setiosflags(ios::showpos) 输出正数时显示"+"号
setiosflags(ios::showpoint) 强制显示小数点
resetiosflags() 终止已经设置的输出格式状态,在括号中应指定内容
举例说明:
oct 八进制
int n=199;
cout<<oct<<n<<endl; 输出:307
dec 十进制
hex 十六进制
int n=199;
cout<<hex<<n<<endl; 输出:c7
setw(n)//设置域宽为n个字符
setfill('&')//填充字符,一般与setw搭配使用
cout<<setfill('&')<<setw(4)<<11<<endl;
输出:&&11
setprecision(n) //设置显示有效数字为n位,会有四舍五入
float n=19.12345600;
cout<<setprecision(1)<<n<<endl;输出:2e+01
cout<<setprecision(2)<<n<<endl;会输出19
cout<<setprecision(3)<<n<endl;会输出19.1
cout<<setprecision(6)<<n<<endl;会输出19.1234
cout<<setprecision(7)<<n<<endl;会输出19.12345
cout<<setprecision(8)<<n<<endl;会输出19.123456
//小数后的0要想输出,需要加showpoint
double n=19.12345600;
cout<<showpoint<<setprecision(10)<<n<<endl;输出:19.12345600
或cout<<setiosflags(ios::showpoint)<<setprecision<<(n)<<n<<endl;
//要想保留小数点后n位数,需要加fixed
double n=19.12345678;
cout<<fixed<<setprecision(n)<<n<<endl;
或cout<<setiosflags(ios::fixed)<<setprecision<<(n)<<n<<endl;
C++ iomanip函数库使用说明
最新推荐文章于 2022-04-25 09:00:02 发布