⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commaflt.c

📁 C语言库函数的源代码,是C语言学习参考的好文档。
💻 C
字号:
/* +++Date last modified: 05-Jul-1997 */

/*
**  COMMAFLT.C - Format a double using commas as thousands separators
**               and a specified number of significant fractional digits.
**
**  public domain by Bruce Wedding and Kurt Kuzba
*/
#include <stdio.h>

#include "numcnvrt.h"
#include "snip_str.h"

char *comma_float( double num, char *buf, int dec)
{
      char tmp[80];
      int bf = 0, cm = 0, tm = 9 - dec + (!dec);

      sprintf(tmp, "%.9f", num);
      strrev(tmp);
      if(dec)
      {
            while( (buf[bf++] = tmp[tm++]) != '.')
                  ;
            while((buf[bf++] = tmp[tm++]) != 0)
            {
                  if(++cm % 3 == 0 && tmp[tm])
                        buf[bf++] = ',';
            }
      }
      return strrev(buf);
}

#ifdef TEST

#include <stdio.h>

int main( void )
{
      int i;
      char result[80];
      double num[10] = {
            1.98765,       12.98765,       123.98765,
            1234.98765,    12345.98765,    123456.98765,
            1234567.98765, 12345678.98765, 123456789.98765,
            1234567890.98765  };

      for ( i = 0; i < 10; i++ )
            printf("Before: %-24.5f After: %s \n",
                  num[i], comma_float(num[i], result, 6));
      return 0;
}

#endif /* TEST */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -