如何在C从一个1123456789号格式为1123456789? [英] How to format a number from 1123456789 to 1,123,456,789 in C?

查看:198
本文介绍了如何在C从一个1123456789号格式为1123456789?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用C语言格式的数量从 1123456789 1123456789
我试着用的printf(%'10D \\ N,1123456789); 但是,这并不正常工作

How can I in C language format a number from 1123456789 to 1,123,456,789? I tried using printf("%'10d\n", 1123456789); but that doesn't work.

您能指点什么?越简单的解决方案更好。

Could you advice anything? The simpler the solution the better.

推荐答案

如果您的printf支持 标志,你可能只需要通过适当地设置您的区域做。例如:

If your printf supports the ' flag, you can probably do it just by setting your locale appropriately. Example:

#include <stdio.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_NUMERIC, "");
    printf("%'d\n", 1123456789);
    return 0;
}

和建&放大器;运行:

$ ./example 
1,123,456,789

测试在Mac OS X和放大器; Linux操作系统(Ubuntu的10.10)。

Tested on Mac OS X & Linux (Ubuntu 10.10).

这篇关于如何在C从一个1123456789号格式为1123456789?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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