464,475 Members | 1,075 Online Need help? Post your question and get tips & solutions from a community of 464,475 IT Pros & Developers. It's quick & easy.

# Multiplying money by 100 - loss of pennies

 P: 12 Hello! I am having issues with my code. My program is supposed to take a given amount of money (1.33) and tell how many quarters, dimes, nickels and pennies would make up this sum of money. Problem is when I multiply the input by 100, depending on the amount of money entered, I lose a penny. When I enter .34 it comes out correct, but when I enter .35, it gives me 34. Any and all help is greatly appreciated. Here is part of my code: Expand|Select|Wrap|Line Numbers     void ChangeMaker::Calc()     {         int q = 25;         int d = 10;         int n = 05;         int p = 01;         int remainderQ = 0;         int remainderD = 0;         int remainderN = 0;             int newAmount = static_cast(amount * 100);         cout << newAmount << endl;         if(newAmount > .24)         {             quarters = (newAmount/q);             remainderQ = (newAmount % q);         }         if(remainderQ > .09)         {             dimes = (remainderQ/d);             remainderD = (remainderQ % d);         }         if(remainderD > .04)         {             nickels = (remainderD/n);             remainderN = (remainderD % n);         }         if(remainderN > .00)         {             pennies = remainderN;         }     }   Mar 26 '08 #1
4 Replies

 Expert Mod 5K+ P: 9,197 int newAmount = static_cast(amount * 100); cout << newAmount << endl; if(newAmount > .24) Err....newAmount is an int. Ints do not have decimal places. You mean: [code=cpp if (newAmount > 24) etc... [/code] And take that cast out of there. Do everything in pennies. Certainly you can write a function with an int argument that can display an int value of 123 as \$1.23. You do not need decimals in a fincance application. They are just eye candy. Mar 26 '08 #2

 P: 12 "amount" is a double. When the user enters an amount, they can enter 5.23. Then what I'm trying to do is take that 5.23 and multiply by 100 so I can work with the modulus operator. Can you show me a better way? Thank you! Err....newAmount is an int. Ints do not have decimal places. You mean: [code=cpp if (newAmount > 24) etc... [/code] And take that cast out of there. Do everything in pennies. Certainly you can write a function with an int argument that can display an int value of 123 as \$1.23. You do not need decimals in a fincance application. They are just eye candy. Mar 26 '08 #3

 Expert Mod 5K+ P: 9,197 Your amount should be int. Let's say the user enters 5.24. You parse the input to the decimal point, pick out the numbers to the left of the decimal, convert them to an int, multiply by 100, put that in the amount then and add the two digits after the decimal to get 524. The code in main() would look like: Expand|Select|Wrap|Line Numbers char buffer; cin.getline(buffer,80); int amount = Convert(buffer);   and off you go. Floating point is intended for scientific use where accuracy is not required. The automatic rounding of floating point can throw off a financial application. Mar 26 '08 #4

 P: 12 Wow! Forgive me for being so uneducated, but that went way over my head. I think I get the big picture though. I've changed everything to ints and it seems to be working properly. Thank you for your help. Mar 26 '08 #5 