5

This function exists on OS X and allows you to pass custom local to the function. setlocale is not thread-safe, and passing locale as parameter is.

If there is no equivalent, any way of locale-independent printf, or printf just for doubles (%g) will be ok.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65

3 Answers3

4

You might try uselocale, which sets the thread locale (rather than the process locale) before calling printf.

Sunlight
  • 2,103
  • 13
  • 9
  • Not perfect, but works. If anyone else has better answer, go ahead. – Paweł Hajdan Nov 07 '08 at 16:29
  • @Sunlight: I think you should amplify your answer a little - but I think you're probably right. @phjr: the BSD/MacOS X solution with printf_l() is arguably tidier, but the uselocale() should work. – Jonathan Leffler Nov 07 '08 at 17:17
  • Note that this solution is ok if you control the whole app, but if you are writing a library routine, it is probably not wise to change the locale process-wide. Hence the desire to want to do formatting in a particular explicit local (or locale-independently, by using the "C" locale). – Larry Gritz Nov 18 '19 at 07:13
1

There are locale-independent double to string convertion routines at http://www.netlib.org/fp/. String to double conversion is available too. The API is not very nice, but the code works.

Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
0

There is a whole family of locale-independent sprintf family of routines as stb_sprintf: https://github.com/nothings/stb It's dual licensed as public domain (where possible) and MIT license.

Larry Gritz
  • 13,331
  • 5
  • 42
  • 42