When I call my function:
formatCurrency(7.5);
string formatCurrency(double cash) {
cout << "fmod(cash,.1) is equal to " << fmod(cash,.1) << endl;
if(fmod(cash,1) == 0) {
cout << cash << ".00";
}
else if(fmod(cash,.1) == 0.1) {
cout << cash << "0";
}
else if(fmod(cash,.01) == 0.01) {
cout << cash;
}
else{
cout << "Error: unable to display in currency format";
}
return "";
}
fmod(7.5,.1) is clearly equal to .1, and it even outputs as such when I run the program. But instead I get the following output:
fmod(cash,.1) is equal to 0.1
Error: unable to display in currency format
What gives? I see literally nothing wrong with my code. The code does work with whole numbers/the first if statement, but anything with a decimal makes things get dicey.
Aucun commentaire:
Enregistrer un commentaire