C++ I/O流的常用控制符

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航