473,371 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,371 software developers and data experts.

Multiplying money by 100 - loss of pennies

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
  1.     void ChangeMaker::Calc()
  2.     {
  3.         int q = 25;
  4.         int d = 10;
  5.         int n = 05;
  6.         int p = 01;
  7.         int remainderQ = 0;
  8.         int remainderD = 0;
  9.         int remainderN = 0;
  10.  
  11.  
  12.         int newAmount = static_cast<int>(amount * 100);
  13.         cout << newAmount << endl;
  14.         if(newAmount > .24)
  15.         {
  16.             quarters = (newAmount/q);
  17.             remainderQ = (newAmount % q);
  18.         }
  19.         if(remainderQ > .09)
  20.         {
  21.             dimes = (remainderQ/d);
  22.             remainderD = (remainderQ % d);
  23.         }
  24.         if(remainderD > .04)
  25.         {
  26.             nickels = (remainderD/n);
  27.             remainderN = (remainderD % n);
  28.         }
  29.         if(remainderN > .00)
  30.         {
  31.             pennies = remainderN;
  32.         }
  33.     }
  34.  
Mar 26 '08 #1
4 1936
weaknessforcats
9,208 Expert Mod 8TB
int newAmount = static_cast<int>(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
ejack
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
weaknessforcats
9,208 Expert Mod 8TB
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
  1. char buffer[80];
  2. cin.getline(buffer,80);
  3. int amount = Convert(buffer);
  4.  
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
ejack
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

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing the code. Thank you. .. Facundo
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
16
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
12
by: David Garamond | last post by:
What do people recommend for storing money amounts? I've seen people use NUMERIC(18,3) and other use NUMERIC(18,4). Which one is more appropriate and why? This is considering various existing...
8
by: John | last post by:
Hi I am calculating tax as follows; = * / 100# Client complains that there is sometimes difference in pennies in tax calculation. Is there a better (more precise) way to calculate tax? ...
45
by: Master Programmer | last post by:
- Constant language / system changes - Dropping of the VB language - Security configuation minefields - Loss of old code base - Time consuming to learn (then it changes every 5 minutes) - Slow...
9
by: bb | last post by:
Hi, Are there any open source data types ( c++ classes ) for Money and/or Price that I can inherit and customize for my use instead of writing from the scratch? Any help would be much...
7
KoreyAusTex
by: KoreyAusTex | last post by:
I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice. This is what I have so far but I have a rounding error when I use the...
1
by: =?Utf-8?B?dHRocm9uZQ==?= | last post by:
Hi, I am querying sql server and loading the resulting data into Excel. For character and integer data types, it's working well, but when I pull decimal data and attempt to load it into Excel...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.