使用流操作算子
头文件 : #include< iomanip >
#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
return 0;
}
输出16进制
//hex
//输出16进制
cout << "521的十六进制为: " << hex << 521 << endl;
cout << "15的十六进制为: ";
cout << hex;
cout << 15;
cout << endl;
输出8进制
//oct
//输出8进制
//方法同16进制
普通小数形式输出
//fixed
//以普通小数形式输出浮点数(6位小数)(位数不够补零)
cout << fixed << 521.1314 << endl;
科学计数法输出
//scientific
//以科学计数法方式输出
cout << scientific << 521.1314 << endl;
左对齐
//left
//左对齐
cout << left << 521.1314 << endl;
右对齐(默认对齐方式)
//right(是默认对齐方式)
//右对齐
//使用同左对齐
设置输出进制
//setbase(b)
//设置输出进制形式
cout << setbase(8) << 521 << endl;
cout << setbase(10) << 521 << endl;
cout << setbase(16) << 521 << endl;
设置输出宽度
//setw(w)
//指定输出宽度
cout << setw(10) << 521.1314 << endl;
设置填充字符(在设置宽度的情况下位数不够)
//setfill(w)
//设置填充字符
cout << setw(10) << setfill('*') << 521.1314 << endl;
设置浮点数精度(可与fixed、scientific一起用)
//setpercision
//设置浮点数精度
//在未使用fixed或scientific情况下设置的是有效位数
//在使用fixed或scientific情况下设置的是小数位数
cout << setw(12) << setfill('*') << setprecision(5) << 521.1314 << endl;
cout << setw(12) << setfill('*') << fixed << setprecision(5) << 521.1314 << endl;
return 0;
使用setiosflags()算子
使用格式:cout<<setiosflags(ios :: …| ios :: …| ios :: …);
这里直接借用站长严长生的图:
给个使用例子:
cout << setiosflags(ios::dec | ios::left | ios::showpos) << 521.1314 << endl;
使用cout的成员函数
设置浮点数的精度(默认是有效数字位数)
//cout.precision(n)
//设置浮点数的精度(默认有效位数)
//在同时使用了fixed后从有效位数变为小数位数
cout.precision(5);
cout << 21.1314 << endl;
cout << fixed << 21.1314 << endl;
设置输出字符的宽度
//cout.width(w)
//设置输出的宽度
cout.width(8);
cout << 21.1314 << endl;
填充字符
//cout.fill(c)
//位数不够填充字符
cout.fill('*');
cout.width(8);
cout << 21.1314 << endl;