10 years, 1 month ago.

Comma as decimal point

I'm working on a project with a multilingual GUI. If the language is set to a European language I want to show floats on the screen with a comma as the decimal point instead of a full stop (i.e. 99,9 instead of 99.9). Does anyone know how I can change the decimal point character at runtime?

Edit: We also need to write floating point values to a csv file, switching between comma and full stop for the decimal separator, and semicolon and comma for the value separator.

Thanks

1 Answer

10 years, 1 month ago.

I googled to this: http://stackoverflow.com/questions/14753505/c-c-printf-use-commas-instead-of-dots-as-decimal-separator. It compiles didn't check if it does what it needs to do.

Hi Erik. Unfortunately this doesn't work. Only the default locale is included, so anything other than setlocale(LC_ALL, "C") or setlocale(LC_ALL, "") returns a NULL pointer.

posted by Tim Barry 16 May 2014

Hmmm then that is an issue.

You can make a manual function to do it like you want. Or for example use sprintf to specifically convert the float to a char array, and in that array search the dot and replace it with a comma.

Btw does it really need to be a comma? If that is a hard requirement from your boss/client then I guess it needs to be a comma. I can't talk obviously for every European country, but here no one is going to be confused by a dot instead of a comma, we use both, sometimes simple lcd screens can't even make a comma.

posted by Erik - 16 May 2014

I should have mentioned that we're also writing data to a csv file. For the English version a period is used for the decimal separator and a comma to separate values in the file. For the European version we'll have to change this to a comma for the decimal separator and a semicolon to separate values. I think I'll have to go with your suggestion of writing data to a buffer and then parsing it before using fprintf. Thanks for looking into it

posted by Tim Barry 16 May 2014