I/O流的常用控制符:
dec 置基數爲10
hex 置基數爲16
oct 置基數爲8
setfill(c) 設填充字符爲C
setprecision(n) 設顯示小數精度爲n位
setw(n) 設域寬爲n個字符
setiosflags(ios::scientific) 指數表示
setiosflags(ios::left) 左對齊
setiosflags(ios::right) 右對齊
setiosflags(ios::skipws) 忽略前導空白
setiosflags(ios::uppercase) 16進制數大寫輸出
setiosflags(ios::lowercase) 16進制數小寫輸出
如下例:
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
double amount = 22.0/7;
int number = 1001;
cout << amount << endl;
cout << setprecision(0) << amount << endl
<< setprecision(1) << amount << endl
<< setprecision(2) << amount << endl
<< setprecision(3) << amount << endl
<< setprecision(4) << amount << endl;
cout << setiosflags(ios::fixed);
cout << setprecision(8) << amount << endl;
cout << "Decimals:" << dec << number << endl
<< "Hexadecimal:" << hex << number << endl
<< "Octal:" << oct << number << endl;
cout << setiosflags(ios::scientific) << amount << endl;
cout << setprecision(6);
system("pause");
}
運行結果爲:
3.14286
3
3
3.1
3.14
3.143
Decimal:1001
Hexadecimal:3e9
Octal:1751
3.14285714
3.14285714e + 00