printf逗号格式化问题 [英] printf comma formatting question

查看:179
本文介绍了printf逗号格式化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何使用printf编写123456格式为123,456的数字?


问候,


... ab

解决方案

3月28日上午8:50,abubak ... @ gmail.com写道:




如何使用printf编写123456格式为123,456的数字?


问候,


..ab



是这样的吗?


int n = 123456;

printf("%d,%d",n / 1000,n%1000);


ab******* @ gmail.com 写道:




如何使用printf编写格式为123,456的数字123456?



int d = 123456;

printf("%d,%d",d / 1000,d%1000);


虽然如果你打算将它用于123456以外的数字,

你可能想要使用10而不是1000的其他功率和

确保模数的结果是正的。


HTH,

- J.


3月28日上午8:50,abubak ... @ gmail.com写道:


如何写一个数字123456使用printf格式化为123,456?



通过设置适当的区域设置。使用printf,您必须修改全局语言环境才能执行此操作,并修改全局数据中存在的所有问题。你最好使用

ostream,你可以设置特定于流的区域设置。


-

James Kanze(GABI软件)mailto:ja ********* @ gmail.com

Conseils en informatique orient?e objet /

Beratung in objektorientierter Datenverarbeitung

9 place S?mard,78210 St.-Cyr-l''?cole,France,+ 33(0)1 30 23 00 34


Hi,
How can I write a number 123456 formatted as 123,456 using printf?

Regards,

...ab

解决方案

On Mar 28, 8:50 am, abubak...@gmail.com wrote:

Hi,
How can I write a number 123456 formatted as 123,456 using printf?

Regards,

..ab

like this?

int n = 123456;
printf("%d,%d", n / 1000, n % 1000);


ab*******@gmail.com wrote:

Hi,
How can I write a number 123456 formatted as 123,456 using printf?

int d=123456;
printf("%d,%d",d/1000,d%1000);

Though if you intend to use it for numbers other than 123456,
you might want to use another power of 10 instead of 1000 and
make sure that the result of the modulo is positive.

HTH,
- J.


On Mar 28, 8:50 am, abubak...@gmail.com wrote:

How can I write a number 123456 formatted as 123,456 using printf?

By setting an appropriate locale. With printf, you have to
modify the global locale to do this, with all of the problems
inherent in modifying global data. You''d be better off using
ostream, where you can set a locale specific to the stream.

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l''?cole, France, +33 (0)1 30 23 00 34


这篇关于printf逗号格式化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆