#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a = 20;
cout << oct << a << endl; //设置整数为八进制
cout << dec << a << endl; //设置整数为十进制
cout << hex << a << endl; //设置整数为十六进制
cout << a << endl; //此时为十六进制
cout << setbase(8) << a << endl;
cout << setbase(10) << a << endl;
cout << setbase(16) << a << endl; //设置整数为n进制(n=8,10,16)
cout << a << endl; //此时为十六进制
double pi = 3.141592653589793238462643383279;
cout << pi << endl; //默认有效位数6位
cout << setprecision(3) << pi << endl;
cout << setprecision(5) << pi << endl;
cout << setprecision(15) << pi << endl; //设置有效位数,n与浮点数常量的位数取小的值
cout << pi << endl; //此时保留十五位,包括小数点前后
double b = 27.09790134;
double c = 4656.867883679;
double d = 878729.43467687299;
cout << fixed << setprecision(3) << b << endl;
cout << fixed << setprecision(4) << c << endl;
cout << fixed << setprecision(5) << d << endl; //设置有效小数点后位数
cout << d << endl; //此时小数点后保留五位
double p = 1234.9;
cout << setfill('*') << setw(20) << p << endl;
cout << setfill('*') << setw(20) << left << p << endl; //setw(n)个字符左填充
cout << setfill('*') << setw(20) << right << p << endl; //setw(n)个字符右填充,默认右填充
double k = 98366568.897328686;
cout << scientific << setprecision(6) << k << endl; //科学计数,此时n为小数位数
/*
此外,一旦设定,之后的输出将按照最后设定的格式。
*/
}
C++的标准库iomanip
于 2022-07-16 15:28:40 首次发布