This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculating Change

 
Mark Captain
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im trying to calculate the amount of each coin given in change and to have the least amount of coins returned. I have a formula going, however, it is only calculating the amount of each coin seperatly. Say there was 9.50 in change, it is returning me change = 9 dollars, 38 quarters, 95 dimes, 190 nickels, 950 pennies. While the answer i was looking for would be 9 dollars, 2 quarters, 0 dimes, 0 nickels, 0 pennies.

It looks as if every time a method runs, the change does not transfer over. Im stuck on how to fix this

The code i have in the main driver is



and the methods from the Machine class

 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you done so far to isolate the problem? Have you stepped through the code with the IDE's debugger? or if you don't have a good IDE, have you put in System.out.println() statements
in the code? Have you debugged one case yet?

A critical aside, not directly related to your problem, but I have to mention it: never use float or double for money. Ever. Use the integer number of pennies. Floating point numbers don't add up the way pennies do. The problems will be subtle and hard to find, but some accountant will find it and jump all over your, er, back.


 
Winston Gutkowski
Bartender
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:A critical aside, not directly related to your problem, but I have to mention it: never use float or double for money. Ever. Use the integer number of pennies.

Or alternatively BigDecimal.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic