Also, **using floats for money is a bad idea!** Money is *not* floating point; it's fixed precision.

Try this:

Code:

#include <stdio.h>
int main() {
float x=0.0f, i;
for (i=0.0f; i<20; i+=0.1f) {
printf("%f\n",x+i);
}
return 0;
}

Depending on whether you system is 32 or 64 bit, at some point you will see the expected (an increase of 0.1 at each iteration) become the unexpected:

**2.500000**

2.600000

2.700000

2.799999

2.899999

2.999999

3.099999

3.199999

This has to do with the nature of floating point numbers. You may think this is okay, since you are going to round it anyway, but actually the opposite it true: if you do enough arithmetic with floats like this, you can end up losing pennies.

When dealing with money, always use an int value, the number of cents, and convert it using division (and modulus for the remainder) for output:

Code:

int val = 532; /* number of cents */
printf("$%d.%2d\n",val/100,val%100);