私は、double 型の多くの数値 (小数点の右側にわずか数桁の数百万と数十億の値) を持つ C++ プログラムに取り組んでいます。これらの数値に対して計算を実行し、結果をテキスト/CSV ファイルに出力しています。テキスト ファイルで、すべての数値が (6 桁に) 丸められているように見えることに気付きました。したがって、13,169,911 という値は、出力ファイルでは 13,169,900 として表示されます。
この丸めは印刷物でのみ発生しますか? 変数の全桁数を取得するには、ファイルに書き込むときに何かを指定する必要がありますか? 以下に、ファイルへの書き込みコードのサンプルを含めました。
void PrintPropFinance(vector<PropFinance>& PF, int NumProps, int Iterations, int ForecastLength,
string CurDeal, string ModelRunID, string ScenName, Assumptions& Ass) {
string filename;
ofstream OutFile;
ostringstream s1;
s1 << BASEPATH << "Output/" << CurDeal << "_" << ModelRunID << "_" <<
ScenName << "_PropFinance" << ".csv";
filename = s1.str();
OutFile.open(filename);
// Put in the column headers first
OutFile << "PropID" << ","
<< "Item" << ","
<< "StartDate" << ","
<< "NumOfPeriod" << ","
<< "Result" << ","
<< "Isap" << ","
<< "CurLoanBal" << ","
for (int i=1; i<=NumProps; ++i) {
// Populate the single-vector variables
OutFile << PF[i].PropID << ","
<< PF[i].Item << ","
<< PF[i].StartDate << ","
<< PF[i].NumOfPeriod << ","
<< PF[i].Result << ","
<< PF[i].Isap << ","
<< PF[i].CurLoanBal << ","
<< endl;
}
OutFile.close();
}
// Prop finance class definition
class PropFinance {
public:
string PropID;
int Item;
string StartDate;
int NumOfPeriod;
string Isap;
double CurLoanBal;
}