【问题标题】:How can I get rid of zeros, if decimal is entered it prints decimal, but if whole number is entered it prints .00?我怎样才能摆脱零,如果输入小数,它会打印小数,但如果输入整数,它会打印 .00?
【发布时间】:2019-10-20 06:39:45
【问题描述】:

此程序打印两个间隔的重叠。但问题是,如果我输入,例如数字: 1.1 -1.1 1.1 1.1,它打印出整数。我尝试在最后一个 printf 命令中编写 %1.1f,但结果更糟,因为如果我输入 1 2 1 1,它会打印出 1.0 和 4.0。如果输入小数或整数,如何获得正确的打印?

#include <math.h>

int main() {
    float a,b,c,x,derivative;
    printf("Input coefficients a, b i c: ");
    scanf("%f %f %f",&a,&b,&c);
    if((a<(-10)) || (a>10) || (b<(-10)) || (b>10) || (c<(-10)) || (c>10)){
        printf("Coefficients a, b i c do not belong in range of (-10,10).");
        return 1;
    }
    printf("Input point x: ");
    scanf("%f",&x);
    derivative=(2*a*x)+b+(c*0);
    printf("First derivation in point x=%.f je %.f.",x,derivative);
    return 0;
}

【问题讨论】:

    标签: c decimal


    【解决方案1】:

    您可以使用"%g" format specifier 以最短的方式显示浮点数:

    printf("First derivation in point x=%g je %g",x,derivative);
    

    【讨论】:

      猜你喜欢
      • 2022-11-04
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      相关资源
      最近更新 更多