2

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.

  • 1
    Will this help - http://stackoverflow.com/a/7276879/763026 – Angshuman Agarwal Oct 08 '12 at 08:39
  • Thanks, but that's very C++. I'd specifically like to know why _sprintf_l does not use the thousands_sep that the locale says it should or if I'm doing something wrong. Not overly used to a WIN32 env. – Claes_Douglas Oct 08 '12 at 09:17

1 Answers1

2

Your code is correct. Amazingly it seems that the "_l" versions simply don't work. And will never work (closed as wontfix!)

https://connect.microsoft.com/VisualStudio/feedback/details/523503/sprintf-l-and-vsnprintf-l-and-relatives-does-not-respect-passed-locale