我如何添加一个千位分隔符在Windows在C双? [英] How can I add a thousands separator to a double in C on Windows?

查看:107
本文介绍了我如何添加一个千位分隔符在Windows在C双?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用MPFR库做的大的数字计算,而且小数点后返回与8位双。

I use the MPFR library to do calculations on big numbers, but also return a double with 8 digits after the decimal point.

我mpfr_sprintf数为char数组,以便precision或任何东西也不会丢失。
一切都很好,只是我没有找到的文档中找到千位分隔符选项(或我错过了)。

I mpfr_sprintf the number to a char array so precision or anything isn't lost. Everything is fine except that I didn't find any thousand separator option in the documentation(or I missed it).

由于一个数字,如 20043.95381376 的我想重新present它喜欢的 20,043.95381376 的可读性更好。

Given a number such as 20043.95381376 I would like to represent it like 20,043.95381376 for better readability.

或数字的 164992818.48075795 的作为的 164,992,818.48075795

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

I read about an apostrophe that should be added to printf/sprintf, but that seems to be a UNIX/POSIX thing and I am a Windows user.

由于内部我打印作为一个字符串的数字,我想我可以做的是写一个自定义的实现,将自动添加取决于数(> 1000> 10000> 100000等)逗号,但后来我意识到,像函数strncpy或strcpy的功能将基本上替换,而不是逗号添加到所需的位置。这里是如何,我又回到了起点上如何做到这一点。

Since internally I print the number as a string, I thought what I could do is write a custom implementation that would automatically add the comma depending on the number(>1000>10000>100000 and so forth) but then I realized that functions like strncpy or strcpy will essentially replace, not add the comma to the desired position. And here is how I am back to square one on how to do it.

我该怎么办呢?

推荐答案

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

You need your implementation to convert the double value to string and examine each character of that string, then copy it to an output string along with the separators.

事情是这样的:

#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;
}

然后使用它像这样:

Then use it like so:

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编译器的工作。

Note: I assumed that if you're on Windows, you may be using MSVC so this solution should be working on C89 compilers.

这篇关于我如何添加一个千位分隔符在Windows在C双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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