C/C++ 保留N位小数

保留N位小数

方法一:使用fixedsetprecision

#include  <iostream>
#include <iomanip> //需要包含此头文件
using namespace std;
int main()
{
    double x=3.1415926;
    int N=3;
    cout <<fixed<<setprecision(N) << x <<endl;
}

方法二:printf ( )

#include <stdio.h>
int main()
{
    dobule a = 1.23456;
    printf("%.2f\n", a);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/explorer9607/article/details/81505680
今日推荐