Set precision with fstream to output file - format double

前端 未结 1 1164

I cannot find an answer for how I might use an already opened fstream to output formatted doubles to a text file. Currently my code is doing this:

int writeT         


        
相关标签:
1条回答
  • 2020-12-19 18:48

    Try using

    #include <iomanip>  // std::setprecision()
    
    f << fixed << setprecision(2) << endl;
    

    set precision sets the number of significant digits, not the number of decimals places. For example

    cout << setprecision(3) << 12.3456 << endl;
    

    would output 12.3
    Sending in fixed first makes it so you are setting the precision from a fixed position (the decimal place) rather than from the first digit of the floating point value.

    0 讨论(0)
提交回复
热议问题