I'm trying to get an output to file such as this:
1.11111
11.1111
111.111
1111.11
111111
In other words, I try to set the significance of my output, instead of my precision. I've tried
oFile <<setprecision(6);
and
fprintf(oFile, "%6f", varName);
To no avail.
Any help is much appreciated.
Cheers
EDIT: Sorry for the incomplete question. Here's a Minimal, Complete, and Verifiable example:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main() {
ofstream oFile("output.dat");
float var1 = 10.9993;
float var2 = 11;
oFile << var1 << endl;
oFile << var2 << endl;
oFile << std::setprecision(6) << var2 - var1 <<endl; //10.9993 - 11 = -0.000699997
oFile.close();
/*
* output
* 10.9993
* 11
* 0.000699997
*/
FILE * oFile2;
oFile2 = fopen("output2.dat","w");
fprintf(oFile2, "%6f \n%6f \n%6f \n",var1, var2, var2-var1);
fclose(oFile2);
/*
* output
* 10.999300
* 11.000000
* 0.000700
*/
}
So I want to have at most 6 significant digits in every case, regardless of the precision, i.e:
10.9993
11 or 11.0000 that does not matter
0.00070
OK, so I've ended up multiplying each variable out of decimal points, subtracted and divided back into decimal points. This seems to work. Crazy that there does not seem to be a functionality to set significance in C++.