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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值