I want to calculate change due, and convert it to pennies.
#include <iostream>
using namespace std;
int main()
{
float cost = 5.15,
paid = 10.00,
change = 0.0;
int pennies = 0.0;
change = paid - cost;
pennies = static_cast<int>(change * 100);
cout << change << endl; //4.85
cout << pennies << endl; //484 ??
return 0;
}
Pennies evaluates to 484, where did my penny go?
I've tried this with float and double, with and without static_cast.
I'm in an intro c++ class so the point is to do this with basic operations.
Dose (4.85 * 100) evaluate to like 484.999999... when using floats so its being truncated?