I/O流的常用控制符

原来是钱能《C++程序设计教程》里的P23和P24的,晕!


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;
int main()
{
    double amount = 22.0/7;
    int number = 1001; 

    cout << amount << endl;
    cout << setprecision(0) << amount << endl;
    cout << setprecision(1) << amount << endl;
    cout << setprecision(2) << amount << endl;
    cout << setprecision(3) << amount << endl;
    cout << setprecision(4) << amount << endl;
    cout << setprecision(8) << amount << endl;

    cout << "Decimals:" << dec << number << endl;
    cout << "Hexadecimal:" << hex << number << endl;
    cout << "Octal:" << oct << number << endl;
    cout << setiosflags(ios::fixed);
    cout << setiosflags(ios::scientific) << amount << endl;

    system("pause");
    return 0;
}

运行结果:
3.14286
3
3
3.1
3.14
3.143
3.1428571
Decimals:1001
Hexadecimal:3e9
Octal:1751
3.1428571

你可能感兴趣的:(ios,C++,c,C#)