cout格式控制

今天花了半天整这个东西,参考http://www.cnblogs.com/walfud/articles/2047096.html 这个网页的内容总结了一下。大部分内容参考该网页

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	
	//numeric(数值),literal(字面值)
	cout << true << " or " << false << endl;               //输出1 or 0   默认数值
	cout << boolalpha << true << " or " << false << endl;  //输出true or false 改为字面值
	cout << true << " or " << false << endl;               //还是输出 true or false 说明是持续生效的
	cout << 0 << endl;                                     //输出0
	cout << boolalpha << 0 << endl;                        //输出0,因为0不等价于false
	cout << noboolalpha << true << " or " << false << endl;  //恢复输出1 or 0    恢复数值

	cout << "-------------------------------------------------" << endl;
	const int ival = 17;                                   // 常量,不允许改变
	cout << "oct : " << oct << ival << endl;               // 21 : 8 进制
	cout << "dec : " << dec << ival << endl;               // 17 : 10 进制
	cout << "hex : " << hex << ival << endl;               // 11 : 16 进制
	cout << "hex : " << hex << 17.01 << endl;              // 17.01 : 不受影响

	cout << "----------------------------------------------------" << endl;
	cout << showbase;
	cout << "oct : " << oct << ival << endl;               // 21 : 8 进制
	cout << "dec : " << dec << ival << endl;               // 17 : 10 进制
	cout << "hex : " << hex << ival << endl;               // 11 : 16 进制
	cout << "hex : " << hex << 17.01 << endl;              // 17.01 : 不受影响

	cout << "----------------------------------------------------" << endl;
	cout << showbase << uppercase;                        //将十六进制符改为大写
	cout << "hex: " << hex << 15 << endl;
	cout << nouppercase;                                  //又改回小写
	cout << "hex: " << 15 << endl;
	cout << noshowbase;                                                                                   
	cout << 15 << endl;                                   //输出 f 说明hex影响还在
	cout << dec;                                          //  恢复对整数用十进制输出
	cout << "-----------------------------------------------------" << endl;

	//对于double型的控制   缺省值默认六位数
	// cout.precision(4);  等价于 cout<<setprecision(4)
	cout << setprecision(2) << 12.345678 << endl;         //把小数全舍掉
	cout << setprecision(4) << 12.345678 << endl;         // 12.35  四舍五入(rounded!)
	cout << setprecision(9) << 12.345678 << endl;         // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
	cout << cout.precision() << endl;                     // 输出当前精度
	cout << setprecision(6);                              //恢复默认值

	cout << "-----------------------------------------------------" << endl;
	float f = 101/6.0;
	cout << f << endl;
	cout << fixed << f << endl;                           //定点数输出,也就是说小数后6位小数(默认值也是)              
	cout << scientific << f << endl;                           
	cout << f << endl;                                    //依然受到scientific的影响
	cout << fixed << f << endl;
	cout.unsetf(ostream::floatfield);                     //恢复到默认值
	cout << f << endl;
	cout << scientific << f << endl;                      //科学计数法也默认 系数用定点数表示
	cout.unsetf(ostream::floatfield);                     //消除对下面的影响
	cout << "-----------------------------------------------------" << endl;

	cout << 10.0 << endl;                                           // 10
	cout << showpoint << 10.0 << endl;                    // 10.0000
	cout << noshowpoint << 10.0<<endl;                    // 恢复到默认处理十进制   10

	cout << "-----------------------------------------------------" << endl;
	
	//输出填充
	//setw(n)为后面一个数字设定宽度,默认右对齐
	cout << setw(10) << 12.3 << endl;                      // ______12.3
	cout << setw(10) << 12 << 3 << endl;                   // ________123
	cout << setw(3) << 12.345 << endl;                     // 实际宽度大于设置宽度,设置宽度无效

	cout << left;                                          // 左对齐
	cout << setw(5) << 12 << setw(5) << 34 << endl;        // 12___34___
	cout << right;
	cout << setw(5) << 12 << setw(5) << 34 << endl;        //  ___12___34

	cout << internal;                                      // By default
	cout << setw(5) << -12 << endl;                        // -__12
	cout << setw(5) << 12 << endl;                         // ___12
	
	cout << setfill('*');                                  // By default
	cout << setw(5) << 12 << endl;                         // ***12


	system("PAUSE");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值