我正在为学校运行一个程序,遇到一个简单的数学问题,C 无法正确计算。我只是想将我的输出除以一个数字,它返回原始数字。
int main(void)
{
float fcost = 599;
float shipt = (fcost / 120);
shipt = shipt * 120;
printf("%4.0f", shipt);
return 0;
}
最佳答案
根据评论中的说明,您希望除法结果向下舍入。
为此,您应该使用 int
而不是 float
,以便执行整数除法(截断小数部分)而不是浮点除法(这会截断小数部分)保留小数部分):
int fcost = 599;
int shipt = (fcost / 120);
shipt *=120;
printf("%d", shipt);
关于C 乘法和除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598062/