I'm getting an error for the value of c when I test this code... when I input '2.10' as the x value, c= x*100 gives me a value of 209?? For other values it seems to work so far, weird. I'm new to coding, any help trying to understand the language is appreciated!

float x;

    printf("O hai! How much change is owed?\n");
 x= GetFloat();

while (x <0);

int c = x*100;

printf("value of change * 100=%d\n", c);

Welcome to the point of this exercise! ;-)

Floats are very rarely stored as exact values in a computer because they have to be converted from base 10 to base 2. If you were to print out the value stored in x, you would see that it is not 2.10, but something very close, like 2.0999999998735 (or something very similar), but ever so slightly less than 2.10. When displayed to 2 decimals, it is rounded to the closest decimal, unless you format it to display enough digits.

When multiplied by 100, it becomes 209.99999998735. The reason that you are getting 209 is that the code is assigning x*100 to c, an int. When a float is assigned to an int, the decimal portion of the number is truncated, so it becomes 209. It is NOT rounded up to 210.

Maybe it's time to explore whether there are any rounding functions and how to use them? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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