### Creating CSV-file with small numbers content

I'm trying to find out how to get the right format when using ofstream when writing to CSV-file.

When writing small numbers e.g. doubles, i want them to look like this 0.00001, and in this way be editable in excel insted of a string that looks like this 1e+5.

 12345678 // pringing the coefficients to file ofstream outFile; outFile.open("coefficients.csv"); for (i=0;i<(n-1);i++){ outFile << a[i] <<";"; // printing coefficients to CSV-file x^0,x^1,x^2,x^3,.... } outFile << a[i] <<"\n"; // printing last coefficients with \n outFile.close();
 #include

 12 // just once, after opening file outFile << fixed << setprecision(5);

Thanks Chervil!

I found out that the problem was in excel. In my country the decimal does start with a "," instead of "." so excel interpretted it as a text string.

Anyway it's interesting with the setprecision(3), it doesn't affect the output:

 123456789 // pringing the coefficients to file ofstream outFile; outFile.open("coefficients.csv"); outFile << fixed << setprecision(3); for (i=0;i<(n-1);i++){ outFile << a[i] <<";"; // printing coefficients to CSV-file x^0,x^1,x^2,x^3,.... } outFile << a[i] <<"\n"; // printing last coefficients with \n outFile.close();

 -1.01737e+255;6.85063e+261;-1.38203e+264;2.40296e+264;-1.10314e+264;-2.40966e+262;1.41064e+263;-4.94294e+262;7.23856e+261;8.72381e+260;-2.81952e+260

Why?
Last edited on
<<";"

Did you mean
<<","

A comma is a semicolon that never grew up.

And for the post that I overlapped with ... are you sure that your coefficients are correct? The magnitudes of those numbers are huge.
Last edited on
In my contry, as I wrote we use "," befor the decimals, and this means that the CSV-files needs to be separated with ";" instead. That's why.

The numbers ar made out of random data and the output isn't relevant in this question.
How would you like a number like -1.10314e+264 to be formatted?
It is correctly formatted but my question is why outFile << fixed << setprecision(3); seems to give no result. In this case I suppose the result sould have been
 -1.1e+264
Last edited on
For a number of that magnitude you probably want scientific, not fixed.
cannot reproduce your issue, provide a testcase.
(note that even setprecision seems to be ignored)

> In this case I suppose the result sould have been
> -1.10e+264
no, that would be scientific with precision 2.
with fixed the result would be -1099999999999999947870924204796281072930243270295235333075692513449338536873729360514597640642232230440436671725593239765694799667225685575602696107581347649216914041553581821371143774609356580328172724615114283621647556821177718905263019987496412950478790496092160.000
no matter what you do in the text file, excel may choose to reformat it for you when it displays it. CSV does NOT store the column type, so excel just takes a best guess at the cell data types, you can over-ride this by converting to xls and setting each column to what you need.

Required reading when messing with CSV files: http://creativyst.com/Doc/Articles/CSV/CSV01.htm

To cause C++ to use your correct, global locale, you must tell it to do so:

 #include
 1234 // Get the current system locale std::locale mycountry( "" ); // (You could also supply a specific locale name here) std::cout.imbue( mycountry ); ...

To print out a fixed-precision number in C++, you need to specify that it is fixed and its widths.

 std::cout << std::fixed << std::setprecision(5);

Finally, to make Excel treat it as a string, you must be specific:

 std::cout << (is_first_field ? "" : ";") << "=\"" << 0.00007 << "\"";

This will produce you properly formatted fields in your record:

="0,00007";="0,01230";="3,14159"

Excel will treat these as strings for display.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.