snprintf : simple way to force . as radix?

后端 未结 3 696
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 05:26

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

相关标签:
3条回答
  • 2021-01-25 05:36

    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.

    0 讨论(0)
  • 2021-01-25 05:37

    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")

    0 讨论(0)
  • 2021-01-25 05:55

    . 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").

    0 讨论(0)
提交回复
热议问题