Search code examples
csetlocale

Programmatically changing the decimal separator with C setlocale()


First of all, this must be really solved in C, and with UNIX standard C functions (because of project constraints). So, C++ or alternative libraries are outside the scope of the question.

I know how to set the default user locale with setlocale, as well as setting the standard C/POSIX locales.

However, I'm in a situation where the decimal separator is file-specified, so I want my program to temporally change the decimal separator.

LC_NUMERIC expects a locale name... but I don't want to give it a locale name, but the separator character directly.

How can this be done?


Solution

  • Well, I'm afraid you won't like the solution :)

    First of all, since you're operating with setlocale you have to supply a locale name. Therefore there should be a locale with LC_NUMERIC property defined by you in the time of program execution. Therefore you need to define a new locale. You may define it with localedef You may use this doc as a guide for making and using a new locale and this site to get source files which you can use as a template for your custom locale definition.