How to output with 3 digits after the decimal point with C++ stream?

前端 未结 4 1019
悲哀的现实
悲哀的现实 2020-12-15 20:51

Given a variable of float type, how to output it with 3 digits after the decimal point, using iostream in C++?

4条回答
  •  渐次进展
    2020-12-15 21:45

    Use setf and precision.

    #include 
    
    using namespace std;
    
    int main () {
        double f = 3.14159;
        cout.setf(ios::fixed,ios::floatfield);
        cout.precision(3);
        cout << f << endl;
        return 0;
    }
    

    This prints 3.142

提交回复
热议问题