Comparing currency values as floats does not work

后端 未结 4 1247
忘了有多久
忘了有多久 2020-12-12 04:54

If I type 0.01 or 0.02 the the program just exits immediately. What do I do?

#include 

char line[100];
float amount;

int main()
{
printf(\"E         


        
4条回答
  •  一生所求
    2020-12-12 05:27

    Declare your comparative value as a float.

    if (amount == 0.01f)
       printf("Quarters: 0\nDimes: 0\nNickels: 0\nPennies: 1");
    

    Note that you could encounter some undefined behavior by directly testing float equality if you made some calculations with your float variable.

提交回复
热议问题