Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculating change in denominations of currency error. Skipping one cents.

 
Rob Burns
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I currently have working code that calculates the denominations that I need when change is displayed. The problem is that it seems to be missing one cents every time. So if my change prints out $12.35, it will end up being 1 - ten, 2 - ones, 1 - quarter, 1 - nickle, and 4 - pennies rather than 2 nickles. or 1 dime This happens for any dollar amount unless it is even cash and no change, or the change calculates to where there doesn't need to be a penny. Just an FYI, this code is being used in a GUI panel program if it seems strange in anyway. I'll list my code below, and if possible, can anyone tell if there is an error somewhere in it?

Thanks for reading
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Burns wrote:So I currently have working code that calculates the denominations that I need when change is displayed. The problem is that it seems to be missing one cents every time. So if my change prints out $12.35, it will end up being 1 - ten, 2 - ones, 1 - quarter, 1 - nickle, and 4 - pennies rather than 2 nickles. or 1 dime This happens for any dollar amount unless it is even cash and no change, or the change calculates to where there doesn't need to be a penny. Just an FYI, this code is being used in a GUI panel program if it seems strange in anyway. I'll list my code below, and if possible, can anyone tell if there is an error somewhere in it?


Assuming that the total and realTotal variables are floating point variables, what you are seeing are rounding errors. And these rounding errors are caused by the way floating point values are stored.

An easy fix would be to ... add a 1/2 penny to the value before converting it to an integer.



Henry
 
Rob Burns
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just tried it and works like a charm! Thank you kindly sir!
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Burns wrote:Just tried it and works like a charm! Thank you kindly sir!


To give a bit more detail... the rounding errors of floating point is actually really small, and should not be noticed in many cases. The problem here is that you are also converting the value to an integer, and to do that, it rounds down to the next whole penny. This means that an error on the negative side, no matter how small, will cause a penny to be lost.

By adding a 1/2 penny, you are changing the algorithm of converting to integer from rounding down to the next whole penny, to rounding to the closest whole penny.

Henry
 
Rob Burns
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, that actually makes a lot of sense. I'll keep that in mind from now on when I am dealing with calculating currency. I really appreciate the help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50273
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two completely accurate ways to calculate currency, and you should use them in preference to floating‑point arithmetic.
  • 1: Denominate all the currency in cents/pence/etc and use integer arithmetic. That does have the risk of overflow for large amounts. £20,000,000 + £2,000,000 as ints will overflow and give an incorrect answer if denominated in pence.
  • 2: Use the built‑in classes for decimal arithmetic. Example here. Read the whole thread
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic