使用std :: ofstream在c ++中格式化float变量输出

obe*_*lix 6 c++ format floating-point ofstream

我有以下代码:

std::ofstream myfile;
std::stringstream filename3;
myfile.open("results.txt");

myfile << "precision= " << precision << "\n";
Run Code Online (Sandbox Code Playgroud)

我文件中的输出格式如下:

precision= 5.96e-07...
Run Code Online (Sandbox Code Playgroud)

如何将数值打印为数字而不是使用e表示的数值?

jro*_*rok 12

使用流操纵器fixed:

myfile << "precision= " << std::fixed << precision << "\n";
Run Code Online (Sandbox Code Playgroud)

您可能还想使用setprecision调整小数位数.