C++中输出 位宽和小数点后位数 的控制

 要用到这个头文件: <iomanip>

                       setw(x) : 表示控制输出x的位宽

                      setprecision(x) :表示 控制输出小数点后 x 位

                      cout.precision(x): 表示控制输出的 该数值的5个数字   

                       例如:y=1.0456789   

                           cout.precision(3);

                          cout<<y<<endl;

                       输出为:1.04 (包含3个数字)

 

#include <iostream>

#include <iomanip>



using namespace std;



int main()

{

	double y=19.0123456789;

    int x=65;



	cout<<setw(5)<<x<<endl;



	cout<<y<<setprecision(4)<<endl ;



	return 0;

}

 

你可能感兴趣的:(C++)