cout输出格式控制

#include <iostream>
#include <string>
#include <vector>
#include<iomanip>
using namespace std;
int main()
{
	//ostream::fmtflags old = cout.flag();        // 无参将返回当前 flag 值
	//cout.flag(old);
	//cout.


	cout << "numeric : " << true << " or " << false << endl;              // 1 or 0
	cout << "literals : " << boolalpha << true << " or " << false << endl; // true or false
	cout << "literals : " << boolalpha << 0 << endl;                     // 0    原因: 0 在cout中不等价于 false
	cout << "numeric : " << noboolalpha << true << " or " << false << endl;// 1 or 0
	cout << endl << endl;

	//====================================================================================================//

	const int ival = 17;        // 'ival' is constant, so value never change
	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;                            // Show base when printing integral values
	cout << "oct : " << oct << ival << endl;        // 21 : 8 进制
	cout << "dec : " << dec << ival << endl;        // 017 : 10 进制
	cout << "hex : " << hex << ival << endl;        // 0x11 : 16 进制
	cout << "hex : " << hex << 17.01 << endl;        // 17.01 : 不受影响
	cout << noshowbase;                            // Reset state of the stream

	cout << endl;
	cout << showbase << uppercase;
	cout << "hex : " << hex << 15 << endl;            // 0XF 大写形式
	cout << nouppercase;
	cout << "hex : " << hex << 15 << endl;            // 0xf 小写形式
	cout << dec;
	
	cout << ival << endl << endl << endl;

	//======================================================================================//

	// cout.pricision(4) ;                         // 等价于 cout <<setprecision(4) ;
	cout << setprecision(4) << 12.345678 << endl;    // 12.35  rounded! setprecision在头文件iomanip中
	cout << setprecision(10) << 12.345678 << endl;   // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
	cout << cout.precision() << endl;               // 输出当前精度
	double dval = 12.13;
	cout << setprecision(1) << dval << endl;
	dval = 12.15;
	setprecision(10);
	cout << dval << endl;
	cout << setprecision(1) << dval << endl;
	cout << setprecision(1) << fixed << dval << endl;
	cout << dval << endl;

	cout << endl;
	float f = 101 / 6.0;
	cout << f << endl;
	cout << fixed << f << endl;           // 16.83334 : 小数点后共6位
	cout << scientific << f << endl;      // 1.683333e+001 : 小数点后共6位

	cout << endl;
	cout.unsetf(ostream::floatfield);  // Retrieve to default handling for notation
	cout << f << endl;                   // 16.8333 : 所有数字共6位

	cout << endl;
	cout << 10.0 << endl;                // 10
	cout << showpoint << 10.0 << endl;    // 10.0000
	cout << noshowpoint << endl;         // Revert to default handling of decimal  

	//================================================================================//
	cout << endl << endl;
	cout << setw(10) << 12.3 << endl;     // ______12.3
	cout << setw(10) << 12 << 3 << endl;   // ________123
	cout << setw(3) << 12.345 << endl;    // If the total output is more than 3, it can be extended

	cout << endl;
	cout << left;                                 // left-justify
	cout << setw(5) << 12 << setw(5) << 34 << endl;   // 12___34___

	cout << endl;
	cout << internal;               // By default
	cout << setw(5) << -12 << endl;   // -___12

	cout << endl;
	cout << setfill('*');          // By default
	cout << setw(5) << 12 << endl;   // ***12

	//==============================================//
	cout << endl << endl;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值