I’m trying to use the Windows specific _sprintf_l
to format a double according to a german locale; that is comma as decimal point and period as thousands separator. In short, I’d like “123456.789” to look “123.456,79” (if printing only two decimals).
However, the thousands separator is not kicking in:
char *loc = setlocale(LC_NUMERIC, "German");
double val = 123456.789;
char buffer[32] = {0};
_locale_t localeinfo;
localeinfo = _get_current_locale();
_sprintf_l(buffer, "%.2f", localeinfo, val);
printf("%s\n", buffer);
Result: “123456,79” (that is a comma as a decimal separator).
Looking at the localeinfo->lconv struct
in the debugger reveals that decimal_point is indeed comma and thousands_sep
is period, so that’s strange.
Any special caveats with _sprintf_l
that I need to handle?
When POSIX/XPG4 is available I can do this:
#define _XOPEN_SOURCE
#include <locale.h>
//
char *s = setlocale(LC_NUMERIC, "De_DE.IBM-1141");
double d = 123456.789;
char s1[100];
printf("%s %'.2f\n", s, d);
...Which prints the expected 123.456,79.
Very grateful for suggestions.