繁体   English   中英

如何在 Windows 上的 C 中为双精度添加千位分隔符?

[英]How can I add a thousands separator to a double in C on Windows?

我使用 MPFR 库对大数进行计算,但也返回小数点后 8 位的双精度数。

我 mpfr_sprintf 将数字转换为字符数组,因此不会丢失精度或任何内容。 一切都很好,只是我在文档中没有找到任何千位分隔符选项(或者我错过了)。

给定一个数字,例如20043.95381376,我想将它表示为20,043.95381376以获得更好的可读性。

或者数字164992818.48075795164,992,818.48075795

我读到应该添加到 printf/sprintf 的撇号,但这似乎是 UNIX/POSIX 的事情,我是 Windows 用户。

由于在内部我将数字打印为字符串,我认为我可以做的是编写一个自定义实现,该实现会根据数字自动添加逗号(>1000>10000>100000 等等),但后来我意识到功能像 strncpy或 strcpy 将基本上替换,而不是将逗号添加到所需位置。 这就是我如何回到如何做到这一点的方式。

我该怎么做?

您需要实现将双精度值转换为字符串并检查该字符串的每个字符,然后将其与分隔符一起复制到输出字符串。

像这样的东西:

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

int thousandsep(double in, char* out_str, size_t out_len, unsigned int precision) {
    char in_str[128], int_str[128], format[32];
    size_t dlen, mod, i, j;
    int c;

    snprintf(format, sizeof format, "%%.%df", precision);
    snprintf(in_str, sizeof in_str, format, in);
    snprintf(int_str, sizeof int_str, "%d", (int)in);

    dlen = strlen(in_str);
    mod = strlen(int_str) % 3;
    c = (mod == 0) ? 3 : mod;

    for (i=0, j=0; i<dlen; i++, j++, c--) {
        if (j >= out_len - 1) {
            /* out_str is too small */
            return -1;
        }

        if (in_str[i] == '.') {
            c = -1;
        } else if (c == 0) {
            out_str[j++] = ',';
            c = 3;
        }

        out_str[j] = in_str[i];
    }
    out_str[j] = '\0';

    return 0;
}

然后像这样使用它:

char out_str[64];

if (thousandsep(20043.95381376, out_str, sizeof out_str, 8) == 0)
    printf("%s\n", out_str);       /* 20,043.95381376 */

if (thousandsep(164992818.48075795, out_str, sizeof out_str, 8) == 0)
    printf("%s\n", out_str);       /* 164,992,818.48075795 */

if (thousandsep(1234567.0, out_str, sizeof out_str, 0) == 0)
    printf("%s\n", out_str);       /* 1,234,567 */

注意:我假设如果你在 Windows 上,你可能正在使用 MSVC,所以这个解决方案应该适用于 C89 编译器。

GetNumberFormatEx将采用数字的纯字符串版本,并使用分组分隔符、适当的小数点等LOCALE_NAME_USER_DEFAULT 。通过LOCALE_NAME_USER_DEFAULT作为区域设置,它将采用用户喜欢的格式。

如果您需要覆盖其中一项设置(如精度),您可以使用默认值填充 NUMBERFMT 结构,然后更改您需要控制的字段。

似乎没有可以使用的格式化指令。

这是一种快速而肮脏的方法,用于获取包含浮点数的字符串并将逗号插入适当的位置。

这使用了几个临时缓冲区。 千位分隔符将取决于区域设置,小数点符号也是如此。 然而,对于这个例子,逗号是硬编码的。

这基本上只是采用浮点数的字符串表示,然后逐步将数字复制到另一个缓冲区并在适当的位置插入逗号。

你也可以像我说的那样用更少的缓冲区来做这件事,这又快又脏,而且效率不高。

{
    double dFloat = 123456789012.567890;
    char xBuff[128];
    sprintf (xBuff, "%f", dFloat);

    char xBuff2[128];
    int iLen = strlen(xBuff);
    int iPoint = iLen;
    for (iLen--; iLen >= 0; iLen--) {
        if (xBuff[iLen] == '.' || xBuff[iLen] == ',') {
            // found the decimal point.  depends on locale.
            iPoint = iLen;
            break;
        }
    }
    strcpy (xBuff2, xBuff + iPoint);   // save the decimal portion

    char xBuff3[128], xBuff4[128];
    xBuff3[127] = 0;    // set an end of string
    int  iCount, jLen;
    for (iCount = 1, jLen = 126, iLen--; iLen >= 0; jLen--, iLen--) {
        if ((iCount % 4) == 0) {
            xBuff3[jLen] = ',';
            jLen--;
            iCount = 1;
        }
        xBuff3[jLen] = xBuff[iLen];
        iCount++;
    }
    strcpy (xBuff4, xBuff3 + jLen + 1);
    strcat (xBuff4, xBuff2);
}
#include <stdio.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_NUMERIC, "");
    printf("%'.8lf\n", 164992818.48075795);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM