I am building a console application that seems to be pulling a lovely trick on me. I ask for a small double number from the prompt and then it sticks that data into a double variable.
Well, when it does this, sayyyy... the number is 2.55, it loads in into the variable as 2.49999999998. Why does it do this? Its really messing with my calculations and I cant find a suitable fix for it.
Brandon
Loading
Posted Jun 27, 2007, 12:55 AM
Mike GoldPosted Jun 26, 2007, 11:32 PM
There is an old math trick:
you can add .5 and then round. This will guarantee it to round to the nearest integer. (this is probably how the Math.Round function works in C#)
e.g.
lngAmountInPennies = (long)(dblAmount*100 +.5d);
I believe this is the solution you are looking for.
Best,
Posted Jun 26, 2007, 1:30 AM
#include
using
namespace std;int
main(){ //Declare variables
double dblAmount;
long lngAmount;
long lngDollars;
long lngQuarters;
long lngDimes;
long lngNickels;
long lngPennies; //Ask for and recieve input
cout << "Please enter a dollar amount: ";
cin >> dblAmount; //Do all calculations
lngAmount = (long)(dblAmount * 100);
lngDollars = lngAmount / 100;
lngAmount -= (lngDollars * 100);
lngQuarters = lngAmount / 25;
lngAmount -= (lngQuarters * 25);
lngDimes = lngAmount / 10;
lngAmount -= (lngDimes * 10);
lngNickels = lngAmount / 5;
lngAmount -= (lngNickels * 5);
lngPennies = lngAmount / 1; //Clear the screen and display new results
system("cls");
cout << "$" << dblAmount << " contains" << endl;
cout << lngDollars << " Dollar(s)" << endl;
cout << lngQuarters << " Quarter(s)" << endl;
cout << lngDimes << " Dime(s)" << endl;
cout << lngNickels << " Nickel(s)" << endl;
cout << lngPennies << " Penny/Pennies" << endl; //Pause the console so results can be viewed
system("pause");
}
I realize that converting a double to a long data type will truncate everything after the decimal, but Ive tried so many different ways to convert this double to a long properly and a few of them work some ways, and other work other ways but I cant seem to find a solid solution that works for all.
The main problem I am having is this: dblAmount is holding 2.549999998. Then I multiply it by 100 so I get 254.9999998. Convert it to a long and it hacks everthing after the decimal so Im left with 254 versus 255, which does make a difference, lol.
Isnt there a way to explicity round a number to a certain decimal place like in C#? Thanks for the help :)
Brandon
Mike GoldPosted Jun 26, 2007, 12:27 AM
Did you mean that when you type in 2.5 you get 2.499999? The reason for this is that double or float representations are never exact. 2.499999 in all essence is 2.5. Sometimes the conversion of the answer will show 2.5000..., sometimes it will show 2.49999..., but your answer will always be limited by the double precision. Integers and longs are safer this way (although they have overflow problems). If you are converting floats or doubles to strings, on the other hand. You should use, for example, myNumber.ToString("0.00") which will round the representation to the second precision padded with two zeroes. If you don't want the 0's, use myNumber.ToString("#.##").
I don't think your calculation is being messed up because of the 2.499999. It would probably be helpful if you posted the
calculation you are trying to perform to see if the accuracy can be improved. You may be losing accuracy do to rounding or by converting from string to double and back.
Below are some rules from Microsoft on using double precision for accuracy:
http://support.microsoft.com/kb/125056