1.通过setprecision()函数 保留有效数字
#include<bits/stdc++.h>
using namespace std;
int main(){
double a = 12.345;
cout << setprecision(3) << a << endl;
return 0;
}
2.通过fixed() 和 setprecision() 保留小数点后有效数字
#include<bits/stdc++.h>
using namespace std;
int main(){
double a = 12.343;
cout << fixed << setprecision(2) << a << endl;
return 0;
}