您的位置:首页 > 编程语言 > C语言/C++

C++ 学习笔记 I/O流的常用控制符

2005-11-05 18:09 671 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: