C++ 输出小数点后 n 位

方法一(也是常用方法):

#include <cstdio> // C++程序中调用标准C的输入输出库

using namespace std;
int main()
{   
double d;   d = 1.65469464;   printf("%lf",d); // 保留默认的小数位(好像是6位)   printf("%.3lf",d); // 指定保留三位小数
  return 0; }
方法二:

#include<iomanip>

using namespace std;

int main()
{
  double d;
  d = 1.65469464;
  cout<<setprecision(3)<<std::fixed<<你的数据; // 指定保留三位小数
  return 0; 
}
原文地址:https://www.cnblogs.com/t-s-y/p/11326124.html