1

My program was not behaving correctly on one machine so I started to hunt for the bug, and I discovered that on that machine, snprintf uses a comma (,), not a . (dot) as 99% of other computers (at least in my experience).

Shouldn't this be standardized?

I am using a library that assumes that the radix is a . (dot) and so it does not work properly with a comma.

So my question is, is there a simple way to force the dot as the radix character? I know I could just search & replace the comma by a dot manually, but surely there is a cleaner way.

houbysoft
  • 32,532
  • 24
  • 103
  • 156
  • The computer is on 64-bit Ubuntu 10.04. The language setting is Czech in which ',' is indeed the radix character. However, this issue is in the "core" of the program, which has two UIs, a CLI and a GUI. If I compile the CLI only, it works fine (and uses the dot). If I compile it with GTK, it breaks and uses the comma. – houbysoft Aug 11 '10 at 12:10
  • `setlocale(LC_NUMERIC, "POSIX")` fixed it, great. Thanks both Doon and manneorama (accepting his, since I never worked with locales and his answer was more explicit). – houbysoft Aug 11 '10 at 12:14

3 Answers3

6

You should be able to change your locale-setting using the setlocale-function to make snprintf use a dot. Try setting it to "POSIX" or "C". (setlocale(LC_ALL, "POSIX")

manneorama
  • 1,291
  • 12
  • 22
5

. versus , is set by the LC_NUMERIC part of the locale. So make sure you set your program to use a locale that uses the . (such as "POSIX").

Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
Doon
  • 19,719
  • 3
  • 40
  • 44
1

For library code, you may wish to use the POSIX 2008 uselocale function instead of setlocale. It is able to set a thread-specific locale, so that if your library code is called from a program that uses threads, it won't mess up the other threads' behavior (and/or crash the program, since setlocale is not thread-safe).

For application code, you should simply avoid ever setting the LC_NUMERIC locale category to anything but C/POSIX. The only categories you really need to set for most applications are LC_CTYPE, LC_MESSAGES, and possibly LC_COLLATE.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711