Set precision with fstream to output file - format double

前端 未结 1 1165

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   // 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)
提交回复
热议问题