(C++)保留小数点位数

需要 iomanip 头文件,在cout后面添加<


例:

#include
#include 
#include
using namespace std;
const double P = 3.1415926;

int main()
{
	double r, c, s;

	cin >> r;        //输入半径
	c = 2 * P*r;      //计算周长
	s = pow(r, 2)*P;  //计算面积
	cout << setiosflags(ios::fixed) << setprecision(2) << c << endl;
	cout << s << endl;

	return 0;
}

(C++)保留小数点位数_第1张图片



你可能感兴趣的:(C++)