0

可能重复:
将 1000000 输出为 1,000,000 等等

我有一个格式为 xxxxxxxx.xx 的浮点变量(例如 11526.99)。我想用逗号将其打印为 11,562.99。如何在 C 中插入逗号?

4

2 回答 2

5

尝试:

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

int main()
{
    float f = 12345.67;

    // obtain the existing locale name for numbers    
    char *oldLocale = setlocale(LC_NUMERIC, NULL);

    // inherit locale from environment
    setlocale(LC_NUMERIC, "");

    // print number
    printf("%'.2f\n", f);

    // set the locale back
    setlocale(LC_NUMERIC, oldLocale);
}

这取决于当前的语言环境。C 和 POSIX 语言环境没有千位分隔符。您可以自己将其设置为您知道使用千位分隔符的语言环境,而不是从环境中继承语言环境。在我的系统上, using"en_NZ"提供了千位分隔符。

于 2013-01-30T04:41:02.680 回答
0

下面的addcommas函数是一个版本locale -less,它允许负浮点数(虽然不适用于指数3.14E10

#include <stdio.h>
#include <string.h>

#define DOT     '.'
#define COMMA   ','
#define MAX     50

static char commas[MAX]; // Where the result is

char *addcommas(float f) {
  char tmp[MAX];            // temp area
  sprintf(tmp, "%f", f);    // refine %f if you need
  char *dot = strchr(tmp, DOT); // do we have a DOT?
  char *src,*dst; // source, dest

  if (dot) {            // Yes
    dst = commas+MAX-strlen(dot)-1; // set dest to allow the fractional part to fit
    strcpy(dst, dot);               // copy that part
    *dot = 0;       // 'cut' that frac part in tmp
    src = --dot;    // point to last non frac char in tmp
    dst--;          // point to previous 'free' char in dest
  }
  else {                // No
    src = tmp+strlen(tmp)-1;    // src is last char of our float string
    dst = commas+MAX-1;         // dst is last char of commas
  }

  int len = strlen(tmp);        // len is the mantissa size
  int cnt = 0;                  // char counter

  do {
    if ( *src<='9' && *src>='0' ) {  // add comma is we added 3 digits already
      if (cnt && !(cnt % 3)) *dst-- = COMMA;
      cnt++; // mantissa digit count increment
    }
    *dst-- = *src--;
  } while (--len);

  return dst+1; // return pointer to result
}

例如,如何调用它(主要示例)

int main () {

   printf ("%s\n", addcommas(0.31415));
   printf ("%s\n", addcommas(3.1415));
   printf ("%s\n", addcommas(31.415));
   printf ("%s\n", addcommas(314.15));
   printf ("%s\n", addcommas(3141.5));
   printf ("%s\n", addcommas(31415));
   printf ("%s\n", addcommas(-0.31415));
   printf ("%s\n", addcommas(-3.1415));
   printf ("%s\n", addcommas(-31.415));
   printf ("%s\n", addcommas(-314.15));
   printf ("%s\n", addcommas(-3141.5));
   printf ("%s\n", addcommas(-31415));
   printf ("%s\n", addcommas(0));

  return 0;
}

编译指令示例

gcc -Wall comma.c -o comma

正在做

./comma

应该输出

0.314150
3.141500
31.415001
314.149994
3,141.500000
31,415.000000
-0.314150
-3.141500
-31.415001
-314.149994
-3,141.500000
-31,415.000000
0.000000
  • 设置DOT为什么是点
  • 设置COMMA为应该是逗号的内容
  • MAX设置为 50 假定转换为字符串的浮点数不会超过 49 个字符(不确定增加MAX
  • 从作为参数给定的浮点数返回指向逗号添加字符串的指针,指向静态区域的指针,因此
    • addcommas不是可重入的,并且返回的指针所指向的值(通常)在每次调用后都会发生变化,例如。
    • 在第二次调用addcommas后, in char *a = addcommas(3.1415) ; char *b = addcommas(2.7182) ; a不能再安全使用
于 2013-01-30T07:07:30.393 回答