Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
5年前关闭。
我有以下代码:
#include <stdio.h>
int main()
{
    int hours;
    float check, phw;
    printf("How many hours worked?");
    scanf(" %d", &hours);
    printf("How much do you make an hour?");
    scanf(" %2f", &phw);

    check = phw * (float)hours;
    printf("You have made $%.2f\n", check);
    return 0;
}

进入时
工作了多少小时二十四
你一小时挣多少钱七点七九
我希望它会出现186.96但我得到:
你赚了168.00美元
程序结束时退出代码:0
我不知道我做错了什么,我刚开始学C,所以任何帮助都是值得感谢的。

最佳答案

scanf(" %2f", &phw);

应该换成
scanf(" %f", &phw);

Here就是一个有效的例子。

关于c - 浮点计算导致错误的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23534756/

10-12 13:30