#配置deovoice cout控制输出小数位数 | blog of coderdz

cout控制输出小数位数

主要是fixedsetprecision两种控制浮点型精度。
具体的看代码注释吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;
}

注意要加头文件#inlcude<iomanip>

谢谢你请我吃苹果!
-------------本文结束感谢您的阅读-------------