Please help! I don't understand why tempCents = 10.00, but when it cast to unsigned, it become 9? WHY?
This only happen if the price = 4.10; If i change it to 3.10, the unsigned cents will be normal '10'.
Code:
int main(void)
{
double price = 0.0;
unsigned dollars;
double tempDollars;
unsigned cents;
double tempCents;
price = 4.10;
dollars = (unsigned) price;
tempDollars = (double) dollars;
tempCents = (price - tempDollars) * 100;
cents = (unsigned) tempCents;
printf("\nPrice: %.2f\n", price);
printf("\nDollars: %u\n", dollars);
printf("\nTemporary Dollars: %.2f\n", tempDollars);
printf("\nTemporary Cents: %.2f\n", tempCents);
printf("\nCents: %u\n", cents);
return EXIT_SUCCESS;
}
Result:
Price: 4.10
Dollars: 4
Temporary Dollars: 4.00
Temporary Cents: 10.00
Cents: 9