如何检查float变量是否包含整数值?到目前为止,我一直在使用:
float f = 4.5886;
if (f-(int)f == 0)
printf("yes\n");
else printf("no\n");
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的解决方案,或者这个解决方案是否存在任何(或许多)缺点.
练习30
编写一个程序,读取以十进制扩展名开发的浮点值
记住数据控制
这是没有关于整数类型的消息的新消息.
#include <stdio.h>
#include <math.h>
int main(){
double x; //the argument of f(x)
printf("Program demands x");
printf("\nand writes the rounded value\n");
printf("Author: xXx\n\n");
//loading data
printf("Write x in float type in decimal extension "); // after many tries, program is not rounding the value
if (scanf("%lf",&x)!=1 || getchar()!='\n'){
printf("Wrong data.\n");
printf("\nEnd of program.\n");
return 0;
}
double round( double x );
printf( "Rounded value is = %lf\n", x);
printf("\nEnd of program.\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)