multiplying a decimal to get whole number not working

Hi all,

I am writing a function to return the number of coins needed to return to user based on the cashing_amount value. The below function works fine unless the last digit is a nine. The problem is that it is rounding down for some reason. For example if the user types 356.69, temp resolves to 0.69, then when it is multiplied by a hundred (expecting 69); the value is 68(?????).

void change(double cashing_amount,int& qtr,int& dm,int& nk,int& pn){

int x = static_cast<int>(cashing_amount);
double temp = cashing_amount-x; //get only decimal value
cout<<"temp: "<<temp<<endl;
int coins = static_cast<int>(temp*100); //make whole number
cout<<"coins: "<<coins<<endl;
qtr=coins/25;
coins%=25;
dm=coins/10;
coins%=10;
nk=coins/5;
pn=coins%5;


}//change coins


WTF - TIA!!!
Read: http://docs.sun.com/source/806-3568/ncg_goldberg.html

Floating point has roundoff issues :/
Topic archived. No new replies allowed.