写了很多遍,一直爱忘记,索性写一篇记下来,再者第一次发的这个月也凑个4篇文章了哈哈~
1.头文件
#include<iostream>
2.精确到小数点后面n位
cout<<setprecision(n)<<fixed<<输出的内容
示例代码
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a = 1;
double b = 1.234;
//setprecision
cout << "a: "<<a << "b: " << b << endl;
cout << setprecision(2) << "b: " << b << " a: " << a << endl;
cout << setprecision(2) << "a: "<<a << " b: " << b << endl;
//小数点加fixed
cout << setprecision(2) << fixed << "b: " << b << " a: " << a << endl;
cout << setprecision(2) << fixed << "a: " << a << " b: " << b << endl;
}