如何使浮点数停止四舍五入为整数?

How does one make a floating point number stop rounding to an integer?

我正在尝试编写一个基本程序,它获取用户的输入,除以七,然后 returns 结果。

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int s = get_int("Input:  ");
    float f = (s / 7);
    printf("%f\n", f);
}

例如。如果我输入“8”,我会期望 1.142857。但我只是得到“1.000000”。这是为什么?

#include <stdio.h>
int main()
{
    int s=4;
    float f = ((float)s / 7);
    printf("%f\n", f);
    getch();
}

您只需将 int 类型转换为 float。 int/float 除法给出给定整数的底数,而 float/float 除法给出浮点数