C++基础学习系列--1、字符输入输出格式设置

#include <IOSTREAM>
#include <IOMANIP>

using namespace std;

int main()
{
	double a = 123.456789012345;
	cout<<"正常输出double a :"<<endl;
	cout<<a<<endl<<endl;

	cout<<"输出前9位数字:"<<endl;
	cout<<setprecision(9)<<a<<endl<<endl;
	
	cout<<"正常输出double a 变成输出9位 :"<<endl;
	cout<<a<<endl<<endl;
	
	cout<<"改回正常输出double a :"<<endl;
	cout<<setprecision(6)<<a<<endl<<endl;
	
	cout<<"设置浮点数固定显示:"<<endl;
	cout<<setiosflags(ios::fixed)<<a<<endl<<endl;

	cout<<"设置浮点数显示8位数显示:"<<endl;
	cout<<setiosflags(ios::fixed)<<setprecision(8)<<a<<endl<<endl;

	cout<<"设置科学技术法显示:"<<endl;
	cout<<setiosflags(ios::scientific)<<a<<endl<<endl;

	cout<<"设置科学技术法显示只显示小数点前4位:"<<endl;
	cout<<setiosflags(ios::scientific)<<setprecision(4)<<a<<endl<<endl;
	
	int b = 123456;
	
	cout<<"输出16进制表示法:"<<endl;
	cout<<hex<<b<<endl<<endl;

	cout<<"输出的16进制数字母大写表示:"<<endl;
	cout<<setiosflags(ios::uppercase)<<b<<endl<<endl;

	cout<<"输出10位长度:"<<endl;
	cout<<setw(10)<<b<<endl<<endl;

	cout<<"输出10位长度,空白格用*号代替:"<<endl;
	cout<<setfill('*')<<setw(10)<<b<<endl<<endl;

	cout<<"输出带正负符号的数字:"<<endl;
	cout<<setiosflags(ios::showpos)<<b<<endl;

	//设置右对齐的函数:setiosflags(ios::right);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值