Hello everyone, in my current project I save my data in excel tables but the problem is that the data is written for example 1.32 and I want to replace all the "." by "," in order to have 1,32 etc for all my recordings.
Do you know a simple way to replace dots with commas on Arduino
In C++ where the OS supports locale, you could use https://cplusplus.com/reference/ios/ios_base/imbue/ but I don't think there is a simple way to configure the decimal separator that I know of on Arduino as the board does not support this
so you need to print the data to a buffer and then replace the dots with commas before printing to the file.
So to rephrase your question, you want to change standard decimal notation to use a comma instead of a period…
You’ll need to write a function to preprocess your float before it’s sent to the file - as a string.
A float can’t contain a comma**, you need to handle that in your own space. Excel won’t have any idea what the number is - if it arrives with a comma in place of the decimal point.
** even as a thousands separator, it is only present in the printed output… not as the number stored in Excel,or any other program.
Why do you need this?
Are you sure that your Excel understands your float numbers with comma?
As far I know, the Excel uses comma as fields separator in i.e. "comma-separated lists". After replacing "1.32" to "1,32" this will be two separate values "1" and "32" instead of float 1.32
Since you imply that you will be opening the files in Excel, just read up in Excel Help how to change the decimal point separator.
OR, you can open the file in a text editor and replace all the points with commas.
It isn't obvious from the code what the file extension used is, Excel's ".csv" extension can use commas to separate items but doesn't have to - there are options in the open dialogue box to select semi-colons for example. I think that .xsl files use tabs as separators but may be misremembered. If you set up your Excel to use comma decimal separators then I suspect that opening a file using points will correctly display them with commas - have a play and find out.
Yep, but I suppose OP has a non-english Excel (e.g. French or Italian...) requiring a comma as decimal separator, and float value with dots won't be properly handled.
So the quickest solution is: myFile.print(String(press_pascal, 2).replace('.', ','));
but as I hate "String" class, I'd do something like this:
char pressure[10]; // Set size to >= total size + 1
// change 8 (total chars) and 2 (decimal digits) as needed
dtostrf(press_pascal, 8, 2, pressure);
*strchr(pressure,'.') = ',';
...
myFile.print(pressure);
in French it would not know what to do with the decimal dot and would handle the comma perfectly fine... I guess that's the need there for one of the European language where we use the comma as decimal separator
when you import your csv in excel you have the option to define the decimal separator in the advanced tab during the import process (at least on my Mac, I would assume microsoft does as well on windows)
otherwise as said in post 7, if you want to only stick to Strings
double press_pascal = 101.325;
String pressure = String(press_pascal, 2); // the float value with 2 decimals
pressure.replace('.', ','); // replace the dot by the comma
file.println(pressure); // print that to the file
I agree, but to do it, he needs to link the CSV to Excel as an external source, making that setting (it'll be persistent for that source), provided the file has exactly the same name in the same position. If the user needs to be free to copy the CSV file on any location, and open it with a simple double click, Excel will open it but uses its standard "localized" format for numbers (not only), failing the correct conversion to floats if "decimal dotted", and interpreted as strings. Unless the user either has an english Excel or set it to permanently use dots instead of commas.
I know it's a silly behaviour forcing a "comma decimal", but Microsoft loves such kind of "translations".. E.g. you know wat is the shortcut key to Find text in an italian WinWord? Guess: Ctrl-F, as in every other software on Earth? Wrong! Ctrl-shift-T as "find" in italian is "Trova"! They're some kinda brain damaged sometimes...
You'd also attach the CSV file, but as far as I can see, the problem is simple.
First, you first need to separate header field names with semicolon, not comma, to have them properly aligned.
Then the main problem is simply due to the fact you need to write records on single lines. So after the last field (humidity) you just need to use "println()" to add the line-terminating characters:
Without it, not only the data will come out as a single row/line, but the last value (humidity), as a string, will be concatenated to the first (time), so you have some very strnge values there!