如何在C中将数字从1123456789格式化为1,123,456,789?

如何用C语言将数字从格式化11234567891,123,456,789?我尝试使用,printf("%'10d\n", 1123456789);但这不起作用。

你能建议什么吗?解决方案越简单越好。


繁星点点滴滴
浏览 918回答 3
3回答

慕姐4208626

如果您的printf支持该'标志(POSIX 2008要求printf()),则可以仅通过适当地设置区域设置来实现。例:#include <stdio.h>#include <locale.h>int main(void){&nbsp; &nbsp; setlocale(LC_NUMERIC, "");&nbsp; &nbsp; printf("%'d\n", 1123456789);&nbsp; &nbsp; return 0;}并运行:$ ./example&nbsp;1,123,456,789在Mac OS X和Linux(Ubuntu 10.10)上进行了测试。

宝慕林4294392

您可以按以下方式递归执行此操作(请注意,INT_MIN如果使用二进制补码,则需要额外的代码来管理它):void printfcomma2 (int n) {&nbsp; &nbsp; if (n < 1000) {&nbsp; &nbsp; &nbsp; &nbsp; printf ("%d", n);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; printfcomma2 (n/1000);&nbsp; &nbsp; printf (",%03d", n%1000);}void printfcomma (int n) {&nbsp; &nbsp; if (n < 0) {&nbsp; &nbsp; &nbsp; &nbsp; printf ("-");&nbsp; &nbsp; &nbsp; &nbsp; n = -n;&nbsp; &nbsp; }&nbsp; &nbsp; printfcomma2 (n);}总结:用户printfcomma使用整数调用,负数的特殊情况是通过简单地打印“-”并使数字为正数来处理(这是不能使用的位INT_MIN)。输入时printfcomma2,小于1,000的数字将被打印并返回。否则,递归将在下一级上调用(因此将调用1,234,567,先以1,234,然后是1),直到找到小于1,000的数字。然后将打印该数字,我们将返回递归树,在打印过程中打印逗号和下一个数字。还有一个更简洁的版本,尽管它在检查每个级别的负数时进行了不必要的处理(这并不重要,因为递归级别的数量有限)。这是一个完整的测试程序:#include <stdio.h>void printfcomma (int n) {&nbsp; &nbsp; if (n < 0) {&nbsp; &nbsp; &nbsp; &nbsp; printf ("-");&nbsp; &nbsp; &nbsp; &nbsp; printfcomma (-n);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; if (n < 1000) {&nbsp; &nbsp; &nbsp; &nbsp; printf ("%d", n);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; printfcomma (n/1000);&nbsp; &nbsp; printf (",%03d", n%1000);}int main (void) {&nbsp; &nbsp; int x[] = {-1234567890, -123456, -12345, -1000, -999, -1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, 1, 999, 1000, 12345, 123456, 1234567890};&nbsp; &nbsp; int *px = x;&nbsp; &nbsp; while (px != &(x[sizeof(x)/sizeof(*x)])) {&nbsp; &nbsp; &nbsp; &nbsp; printf ("%-15d: ", *px);&nbsp; &nbsp; &nbsp; &nbsp; printfcomma (*px);&nbsp; &nbsp; &nbsp; &nbsp; printf ("\n");&nbsp; &nbsp; &nbsp; &nbsp; px++;&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}输出为:-1234567890&nbsp; &nbsp; : -1,234,567,890-123456&nbsp; &nbsp; &nbsp; &nbsp; : -123,456-12345&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: -12,345-1000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : -1,000-999&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: -999-1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: -10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 01&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 1999&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 9991000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: 1,00012345&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 12,345123456&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: 123,4561234567890&nbsp; &nbsp; &nbsp;: 1,234,567,890一个不信任递归的迭代解决方案(尽管递归的唯一问题往往是堆栈空间,这在这里不会成为问题,因为即使对于64位整数也只有几层深度):void printfcomma (int n) {&nbsp; &nbsp; int n2 = 0;&nbsp; &nbsp; int scale = 1;&nbsp; &nbsp; if (n < 0) {&nbsp; &nbsp; &nbsp; &nbsp; printf ("-");&nbsp; &nbsp; &nbsp; &nbsp; n = -n;&nbsp; &nbsp; }&nbsp; &nbsp; while (n >= 1000) {&nbsp; &nbsp; &nbsp; &nbsp; n2 = n2 + scale * (n % 1000);&nbsp; &nbsp; &nbsp; &nbsp; n /= 1000;&nbsp; &nbsp; &nbsp; &nbsp; scale *= 1000;&nbsp; &nbsp; }&nbsp; &nbsp; printf ("%d", n);&nbsp; &nbsp; while (scale != 1) {&nbsp; &nbsp; &nbsp; &nbsp; scale /= 1000;&nbsp; &nbsp; &nbsp; &nbsp; n = n2 / scale;&nbsp; &nbsp; &nbsp; &nbsp; n2 = n2&nbsp; % scale;&nbsp; &nbsp; &nbsp; &nbsp; printf (",%03d", n);&nbsp; &nbsp; }}这两个产生2,147,483,647的INT_MAX。
打开App,查看更多内容
随时随地看视频慕课网APP