C ++在文件输出期间双精度值精度

时间:2014-07-01 14:41:49

标签: c++

我在c ++中有以下代码

ofstream myfile;
    myfile.open ("promise.txt");

    for( int rating=lowerLimit; rating<=upperLimit; rating++ )
    {
        promise = promisePVTable->getPromise(durationTable, rating, duration);

        myfile << "Promise  " << rating << " : " << promise << "\n";
        hurdle = rc_min(1, (promise - 1) / promise);

        hurdles.push_back(hurdle);
    }

每当我输出promise的值时,它会在输出中将值截断为4或5位小数,以为实际值是十进制的16位数。如何在文件输出期间更改double值的精度。

由于

2 个答案:

答案 0 :(得分:1)

使用std::setprecision

#include <iomanip>
//....

myfile << "Promise  " 
       << rating 
       << " : " 
       << std::setprecision(16) << promise 
       << "\n";

答案 1 :(得分:0)

你的ofstream(或者实际上是io_base,来自哪个ofstream)具有&#34;精度&#34;用于设置下一个浮点值的小数精度。

请参阅:http://www.cplusplus.com/reference/ios/ios_base/precision/

所以你必须写:

myfile << "Promise  " << rating << " : ";
myfile.precision(16);
myfile << promise << "\n";