cout控制输出小数位数

主要是fixed和setprecision两种控制浮点型精度。

具体的看代码注释吧:


#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<iostream>
#define rep(i,n) for(int i=1;i<=n;i++)
#define c(ans) cout<<ans<<endl;
using namespace std;
typedef long long ll;

int main(){
	const double x = 5.432153;
	cout << x << endl;
	//默认6精度,所以输出5.43215
	cout << setprecision(4) << x << endl;
	//改为4精度,所以输出5.432
	cout << fixed << setprecision(4) << x << endl;
	//fixed改成控制小数点位数,所以输出5.4322
	cout << x << endl;
	//fixed和setprecision作用还在,输出5.4322
	return 0;
}
注意要加头文件#include<iomanip>.

猜你喜欢

转载自blog.csdn.net/codedz/article/details/79323463