开发者

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 "15开发者_Go百科23556" 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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜