c++流操纵算子基础 简单数字输入输出样例(控制小数点前后位数等)

c++流操纵算子基础 简单数字输入输出样例(控制小数点前后位数等)

所需头文件为 iomanip , io代表输入输出,manip是manipulator(操纵器)

#include<iostream>
#include<iomanip>
using namespace std;

int main(){

	int n=141;
	//分别以十六进制/十进制/八进制先后输出 n;
	cout<<"1)"<<hex<<n<<" "<<dec<<" "<<oct<<n<<endl;//8d 141 215
	double x=1234567.89,y=12.34567;
	//保留5位有效数字;
	cout<<"2)"<<setprecision(5)<<x<<" "<<y<<" "<<endl;//1.2346e+006(整数部分位数大于六计算机自动调用科学计数法,最后一位四舍五入) 12.346
	//保留小数点后5位;
	cout<<"3)"<<fixed<<setprecision(5)<<x<<" "<<y<<endl;//1234567.89000 12.34567
	//科学计数法输出,保留小数点后5位;
	cout<<"4)"<<scientific<<setprecision(5)<<x<<" "<<y<<endl;//1.234557e+006 1.23457e+001
	//非负数要显示正号,输出宽度为12字符(默认右对齐),宽度不足则用*号填补;
	cout<<"5)"<<showpos<<fixed<<setw(12)<<setfill('*')<<12.1<<endl;//***+12.10000
	//非负数不显示正号,输出宽度位12字符,宽度不足则右边用*号填充;
	cout<<"6)"<<noshowpos<<setw(12)<<left<<setfill('*')<<12.1<<endl;//12.10000****
	//输出宽度为12字符,宽度不足则左边用填充字符填充;
	cout<<"7)"<<setw(12)<<right<<12.1<<endl;//****12.10000
	//宽度不足时,负号和数值分列左右,中间用填充字符填充;
	cout<<"8)"<<setw(12)<<internal<<-12.1<<endl;//-***12.10000
	cout<<"9)"<<12.1<<endl;//12.10000
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43256272/article/details/82804513