Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1042951
  • 博文数量: 254
  • 博客积分: 10185
  • 博客等级: 上将
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-25 15:04
文章存档

2011年(8)

2009年(1)

2008年(31)

2007年(214)

分类: C/C++

2007-09-16 22:29:41

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
#include

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

阅读(1176) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~