Chris D.
Chris D.

Reputation: 659

Printing integers with thousands separators in Windows using C

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.

Upvotes: 2

Views: 2636

Answers (2)

Michael Burr
Michael Burr

Reputation: 340486

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)

Upvotes: 3

robert_x44
robert_x44

Reputation: 9324

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.

Upvotes: 1

Related Questions