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

来源:互联网 发布:一梦似瑶台,心知玉女来 编辑:程序博客网 时间:2024/06/17 04:13

需要 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;}





0 0