Working With Floats and Integers

前端 未结 7 738
旧时难觅i
旧时难觅i 2020-12-21 08:40

I\'ve created an ATM like program which holds the money in a person\'s account. When the person takes out a withdrawal, it subtracts the withdrawal from the account along wi

7条回答
  •  礼貌的吻别
    2020-12-21 09:24

    int account = 2000;
    printf("Remaining account: %.2f\n", account);
    

    This is wrong; it should be "%d" for integer, or better, change your account variable type to something that could represent the 0.50 you're surcharging. I don't recommend you to using (imprecise) floats for money either. You don't want to withdraw 10.499999997 when you meant 10.5. You need to think about the precision and rounding rules you'd use. AFAIK these are both mandated by laws or something.

提交回复
热议问题