aspose file tools*
The moose likes Beginning Java and the fly likes Calculating change in denominations of currency error. Skipping one cents. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculating change in denominations of currency error. Skipping one cents." Watch "Calculating change in denominations of currency error. Skipping one cents." New topic
Author

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

Rob Burns
Greenhorn

Joined: May 10, 2014
Posts: 5
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
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Burns
Greenhorn

Joined: May 10, 2014
Posts: 5
Just tried it and works like a charm! Thank you kindly sir!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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

Joined: May 10, 2014
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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
  •  
    jQuery in Action, 2nd edition
     
    subject: Calculating change in denominations of currency error. Skipping one cents.