Printing integers with thousands separators in Windows using C

时光总嘲笑我的痴心妄想 提交于 2019-12-01 09:50:32

问题


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.


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/4109006/printing-integers-with-thousands-separators-in-windows-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!