Way to replace dots with commas on Arduino

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

Thanking you

Where in your example is a dot replaced with a comma?

sorry i just corrected it

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.

1 Like

Thanks for fixing that!

Next question: how will you read Excel tables with an Arduino?

You could do something like this

  • generate the string
  • replace the dot by a comma
  • print the new string to your file
double press_pascal = 101.325;

void setup() {
  Serial.begin(115200);
  String pressure = String(press_pascal, 2);
  pressure.replace('.', ',');
  Serial.println(pressure);
}

void loop() {}

1 Like

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.

EDIT: @J-M-L says the same above.

1 Like

Yeah i see that fonction " replace " on arduino like

//const replaced1 = press_pascal.replace(',', '.');

I saw that you put it in the setup() but think I can directly integrate it into my code like this?

void writeToFile(float press_pascal, float temp_degC, float co2_true, float hydro_true, float humi_pourcent)
{
  myFile = SD.open(filename, FILE_WRITE);
  if (myFile) // it opened OK
  {
    myFile.print(String(hour()));
    myFile.print(":");
    myFile.print(String(minute()));
    myFile.print(":");
    myFile.print(String(second()));
    myFile.print(";");

// ( for each data etc )

String pressure = String(press_pascal, 2);
pressure.replace('.', ',');
Serial.println(pressure);
myFile.print(String(pressure, 2));
myFile.print(";");

    myFile.print(String(temp_degC, 2));
    myFile.print(";");
    myFile.print(String(co2_true, 2));
    myFile.print(";");
    myFile.print(String(hydro_true, 2));
    myFile.print(";");
    myFile.println(String(humi_pourcent, 2));
   
    myFile.close();
  }
  else
    SERIAL_PORT_MONITOR.println("Error opening file");
}```

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);
1 Like

It can be easily configured to use dot for floats rather than comma

1 Like

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

1 Like

Yes I am French and I have problems with points and commas on Excel to draw my curves ^^

Thank you for your solution, I'll try it right away!

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
1 Like

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...

1 Like

re Comma Separator…
Good point !
It’s a big world, and I should have thought harder !
Thanks, but we certainly had the same direction in mind !

1 Like

indeed we did

1 Like

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:

...
    *strchr(humidity,'.') = ',';
    myFile.println(humidity);
   
    myFile.close();
...

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!

1 Like

change this

to

    myFile.println(humidity);

as you want to go to the next line after this

EDIT: same as @docdoc who was faster :slight_smile:

1 Like