Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如果是整数,则使用sprintf格式化不带小数点的浮点_C_Floating Point_Printf - Fatal编程技术网

C 如果是整数,则使用sprintf格式化不带小数点的浮点

C 如果是整数,则使用sprintf格式化不带小数点的浮点,c,floating-point,printf,C,Floating Point,Printf,最初,我使用的是sprintf,浮点值始终为2位小数,代码如下: static void MyFunc(char* buffer, const float percentage) { sprintf(buffer, "%.2f", percentage); } static void MyFunc(char* buffer, const float percentage) { int fractional_part = ((percentage - (int)percentag

最初,我使用的是sprintf,浮点值始终为2位小数,代码如下:

static void MyFunc(char* buffer, const float percentage)
{
    sprintf(buffer, "%.2f", percentage);
}
static void MyFunc(char* buffer, const float percentage)
{
    int fractional_part = ((percentage - (int)percentage) * 100);
    if (0 == fractional_part)
    {
        sprintf(buffer, "%d", (int)percentage);
    }
    else
    {
        sprintf(buffer, "%.2f", percentage);
    }
}
传递的百分比值之一是0x419FFFFF20(调试器视图),它将20.00打印到缓冲区中

当不是整数时,我想显示2位小数,例如

94.74 displayed as 94.74
94.7  displayed as 94.70
0     displayed as 0
5     displayed as 5
100   displayed as 100
我目前正在使用以下代码:

static void MyFunc(char* buffer, const float percentage)
{
    sprintf(buffer, "%.2f", percentage);
}
static void MyFunc(char* buffer, const float percentage)
{
    int fractional_part = ((percentage - (int)percentage) * 100);
    if (0 == fractional_part)
    {
        sprintf(buffer, "%d", (int)percentage);
    }
    else
    {
        sprintf(buffer, "%.2f", percentage);
    }
}
现在,如果0x419FFFFF20(调试器视图)被传递,则小数部分将被计算为99。我假设分数部分的和最终为(19.99-19)*100=99。那么,为什么第一个示例不将19.99打印到缓冲区中


我的问题的正确解决方案是什么?

如果
f
是整数,您可以尝试使用
ceilf(f)==f
floorf(f)==f
返回
true

另一种选择是使用std lib中的
modf(float x,float*ipart)
,或者使用math中的
fmod
,这是一个近似问题

假设百分比为19.999。然后
小数部分将是99,并且将调用浮点分支

但是用两个小数点打印19.999会将其四舍五入到20.00,这就是打印的内容

为了得到一致的结果,您可以始终使用浮点分支,然后在“.”处截断,如果它与“.00”一起出现。否则,您的测试和printf
的内部可能会在某个时候发生冲突

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

int main(int argc, char **argv)
{
        float percentage = 19.999;
        char buffer[50];

        for (percentage = 19.990; percentage < 20.001; percentage += 0.001)
        {
                sprintf(buffer, "%.2f", percentage);
                char *p = strstr(buffer, ".00");
                if (p) *p = 0x0;
                printf("%.3f rendered as %.2f and becomes %s\n", percentage, percentage, buffer);
        }
        return 0;
}

19.990 rendered as 19.99 and becomes 19.99
19.991 rendered as 19.99 and becomes 19.99
19.992 rendered as 19.99 and becomes 19.99
19.993 rendered as 19.99 and becomes 19.99
19.994 rendered as 19.99 and becomes 19.99
19.995 rendered as 19.99 and becomes 19.99
19.996 rendered as 20.00 and becomes 20
19.997 rendered as 20.00 and becomes 20
19.998 rendered as 20.00 and becomes 20
19.999 rendered as 20.00 and becomes 20
20.000 rendered as 20.00 and becomes 20
20.001 rendered as 20.00 and becomes 20

如上所述,可能重复使用“%g”而不是“%d”link@MohamedKALLEL这就是“线程展开”所指的建议。在我看来,它实际上是重复的。我需要2位小数,%.2g,将总位数设置为2,而不是小数位数。执行此操作,我的调试器将给出百分比=0x419FFFFF(20),ceilf(百分比)=0x41A00000(20),floorf(百分比)=0x41980000(19)。它们都不等于原始百分比。它们都不等于原始百分比,所以我的代码执行浮动路径并打印20.00%,这正是我试图避免的。