相关疑难解决方法(0)

检查float是否为整数

如何检查float变量是否包含整数值?到目前为止,我一直在使用:

float f = 4.5886;
if (f-(int)f == 0)
     printf("yes\n");
else printf("no\n");
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更好的解决方案,或者这个解决方案是否存在任何(或许多)缺点.

c floating-point int

65
推荐指数
5
解决办法
9万
查看次数

C - 如何检查数字是整数还是浮点数?

练习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)

c floating-point integer

1
推荐指数
2
解决办法
3万
查看次数

标签 统计

c ×2

floating-point ×2

int ×1

integer ×1