:( input of 4.2 yields output of 18
   \ expected output, but not "22\n"

I believe I have used the roundf() correctly when converting dollars to cents, but it still gives 22 coins total, instead of 18, when the user inputs 4.2 as the change owed.

Yet, it handles any other x.2 input correctly. I used the ternary operator, instead of a while loop, for each coin type when calculating how many of each coin to use. Does that matter? Please help! thank you

  • Did you multiply it by 100 before using the roundf()?
    – David
    Commented Mar 4, 2015 at 14:50
  • Thank you! Indeed, I was using the roundf() incorrectly.
    – Hammertime
    Commented Mar 5, 2015 at 16:12
  • I am getting a similar error. However when I run the same numbers through my code in the Application, I get the correct numbers. Does anyone know why this might be?
    – user6564
    Commented Mar 14, 2015 at 0:17

3 Answers 3


This case was specifically entered to illustrate floating point imprecision. You may need to re-watch Zamyla's video walkthrough, paying specific attention to how to properly round the float. It may also help to use ints not floats...

  1. check what is the int value before tertiary operator with printf (should be 420)
  2. if it is correct, output the operands of the tertiary operator.

Try to use round() from math.h

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .