需要 iomanip 头文件,在cout后面添加<<setiosflags(ios::fixed) << setprecision(n) ,n代表保留的小数位数。
例:
</pre><pre name="code" class="cpp">#include<iostream>
#include <cmath>
#include<iomanip>
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;
}