1

The question is pretty self-explanatory, I suppose. I am using printf and friends (snprintf etc) to display some memory statistics that are in the millions or hundreds-of-thousands range. Reading a number formatted like "1,523,556" is much easier than "1523556" to my lazy way of thinking.

I have tried setting the locale and using the apostrophe flag before the format specifier (%'d and %'llu), but the apostrophe is apparently a standard from the SUS, so it may not work for me under Windows anyway.

Is there a Windows-specific API for doing this? I am working with Pelles C and programming in straight ANSI C99.

** EDIT **

After reading the answers and the MSDN pages associated with them, I understand why .NET is the preferred method for Windows programming now. It smooths over a tremendous amount of API work.

Charles
  • 50,943
  • 13
  • 104
  • 142
Chris D.
  • 639
  • 2
  • 8
  • 18
  • 1
    This isn't really answer-worthy, but from the C FAQ: http://c-faq.com/stdio/commaprint.html. I can not find any sort of Windows API that does this for you, and you aren't running a POSIX system that supports those `printf` modifiers. – wkl Nov 05 '10 at 18:46
  • @birryee Thanks for the comment. I had actually found this same page on the C FAQ, which reminded be about the apostrophe from programming under Linux. :-) No such luck on Windows though. – Chris D. Nov 05 '10 at 19:18
  • More related answers can be found here: http://stackoverflow.com/questions/1449805/how-to-format-a-number-from-1123456789-to-1-123-456-789-in-c – Roland Pihlakas Aug 23 '15 at 01:43

2 Answers2

3

The Win32 API does provide a function that will format a number with thousands grouping (or whatever grouping is appropriate for the specified locale): GetNumberFormat() (http://msdn.microsoft.com/en-us/library/dd318110.aspx).

Unfortunately, it's a pretty painful API to use - not nearly as simple as the apostrophe format specifier in SUS (on the other hand, you get a lot of flexibility in exchange for the complexity)

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • This is precisely what I was looking for but couldn't seem to find, thanks. And really most of the regular C Windows API's are painful to use. But powerful, as you say. – Chris D. Nov 05 '10 at 19:20
1

Use GetNumberFormatEx (for Windows Vista and later). The option LOCALE_NAME_USER_DEFAULT uses the preferences set in the Control Panel under regional and language options, including a thousands seperator.

robert_x44
  • 9,224
  • 1
  • 32
  • 37
  • Thank you for the answer! This is a correct and direct answer to my question, but my work computers (only a year old...) still all have XP on them. Pfft. I wish I could accept more than 1 answer. – Chris D. Nov 05 '10 at 19:19