简体   繁体   English

我怎样才能摆脱零,如果输入小数,它会打印小数,但如果输入整数,它会打印.00?

[英]How can I get rid of zeros, if decimal is entered it prints decimal, but if whole number is entered it prints .00?

This program prints overlapping of two intervals.该程序打印两个间隔的重叠。 But the problem is, if I enter, for example numbers: 1.1 -1.1 1.1 1.1, it prints out the whole number.但问题是,如果我输入例如数字:1.1 -1.1 1.1 1.1,它会打印出整数。 I've tried writing %1.1f in last printf command, but it turned out to be even worse, because then, if I enter 1 2 1 1, it prints out 1.0 and 4.0.我尝试在最后一个 printf 命令中编写 %1.1f,但结果更糟,因为如果我输入 1 2 1 1,它会打印出 1.0 和 4.0。 How can I get the proper print if I enter decimale or int?如果输入小数或整数,如何获得正确的打印?

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

You can use the "%g" format specifier to display floating-point numbers in the shortest possible way:您可以使用"%g"格式说明符以最短的方式显示浮点数:

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

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

相关问题 scanf始终打印输入的数字 - scanf always prints entered number 为什么我的程序打印错误的十进制数字 - Why my programm prints wrong decimal number 我对我的输出感到困惑,该输出给出了用户输入的数字的总和的最接近平均值(将小数点四舍五入为整数) - I am confused about my output that gives the closest average of sum of numbers entered by the user.(rounds the decimal to a whole number) 如何将结果输出到用户先前输入的小数点数 (C)? - How do I output a result to the number of decimal-points user previously entered (C)? 如何获取strtok()返回十进制数字而不是整数? - How would I get strtok() to return the decimal number and not whole numbers? 尝试捕捉存储在 long 中的输入值是否为十进制数 C - Trying to catch if the entered value stored in long is a decimal number C 如何逐位获取任意十进制数 - how can I get bit-by-bit of an arbitrary decimal number 如何根据C中的输入,通过减少输入的单词的十进制形式来加密单词并打印? - How do I encrypt words by reducing the decimal form of words entered according to the input in C and print it? 有关将十进制数字打印为二进制的循环的建议 - Advice on this loop that prints decimal numbers into binary 十进制到二进制 - For 循环以反向模式打印二进制 - Decimal to binary - For loop prints the binary in reverse mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM