# change finder

• January 22nd, 2013, 06:13 PM
Darksole
change finder
hey guys below is my code for a change finder the code takes in a sales total and the amount payed and gives a change value and the amount of coins without using nickles. i cant seem to figure out however why the code is one cent short on
Given \$36.0 for a purchase of \$32.27 we need
3 dollars 2 quarters 2 dimes 2 cents

it should be 3 cents not 2 it is right on my other tests however.
here is my code

Code :

```import java.util.Scanner; public class Change { public static void main(String [] args) { double purchase; double pay; double change; int balance; int dollars; int quarters; int dimes; int cents;   Scanner in = new Scanner(System.in); System.out.print("purchase"); purchase = in.nextDouble(); System.out.println("payment"); pay = in.nextDouble();   change = pay - purchase; balance = (int)(change * 100); System.out.println(balance); dollars = balance / 100; balance = (balance - (100 * dollars)); quarters = balance / 25; balance = (balance - (25 * quarters)); dimes = balance / 10; balance = (balance - (10 * dimes)); cents = balance / 1; balance = balance - (1 * cents);       System.out.println("Given \$" + pay + " for a purchase of \$" + purchase + " we need"); System.out.println(dollars + " dollars " + quarters + " quarters " + dimes + " dimes " + cents + " cents");   } }```

any help is appreciated i am stuck as to how to fix it. thanks i look forward to any suggestions.
• January 22nd, 2013, 06:35 PM
Norm
Re: change finder
The code is not properly formatted making it hard to read and understand. All statements should not start in the first column. Nested statements should be indented 3-4 spaces.

Print out the value of change to see what the problem is. There is a problem working with doubles with integer like values. There is a web page somewhere that explains the rounding/precision problems with using doubles for currency computations.