# Problem with Cash return

• 01-27-2014, 07:33 PM
outime
Problem with Cash return
Hello guys,

I'm currently working on an assignment for school where I need to write a program that it needs to return your change in this mode

Change: \$30.5
1 twenty dollar bill
1 ten dollar bill
2 quarters

But, if I work with this case scenario it works fine.

Input:
purchaseAmount: \$12.45
givenAmount: \$15.00

Output:
Change: \$2.55
2 one dollar bills
2 quarters
1 nickel

Now, If I want this scenario to work this what I get

Input:
purchaseAmount: \$19.51
givenAmount: \$50.01

Output:

Twenty dollar bill 1
Ten dollar bill: 1
Quarters: 1
Dimes: 2
Pennies: 4

Which is incorrect since I'm returning to the costumer 30.49 instead of 30.5

Can't figure out whats the issue here.

Thanks

Code:

```      import javax.swing.JOptionPane; public class MoneyCount {         public static void main(String[] args) {                 //booleans         boolean isFivtyOn = false, isFiveBillOn = false, isTwentybillOn = false, isTenbillOn = false, isDollarOn = false, isQuartersOn = false, isDimesOn = false, isNicklesOn = false, isPenniesOn = false;                 //retrieve amount due         String moneyd = JOptionPane.showInputDialog("Enter the amount due");         double amountd = Double.parseDouble(moneyd);                 String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");         double amountr = Double.parseDouble(moneyr);                 //calculate difference         double difference = (amountr - amountd);                 //convert to pennies         int balance = (int)(difference * 100);                 //50 dollar bill         int fivtydollars = balance / 5000;         balance = (balance % 5000);         if (fivtydollars > 0) {             isFivtyOn = true;         }                 //twenty dollar bill         int twentydollars = balance / 2000;         balance = (balance % 2000);         if (twentydollars > 0) {             isTwentybillOn = true;         }                 //ten dollar bill         int tendollars = balance / 1000;         balance = (balance % 1000);         if (tendollars > 0) {             isTenbillOn = true;         }                 //five dollar bill         int fivedollars = balance / 500;         balance = (balance % 500);         if (fivedollars > 0) {             isFiveBillOn = true;         }                 //dollar bill         int dollars = balance / 100;         balance = (balance % 100);         if (dollars > 0) {             isDollarOn = true;         }                 //quartes         int quarters = balance / 25;         balance = (balance % 25);         if (quarters > 0) {             isQuartersOn = true;         }                 //dimes         int dimes = balance / 10;         balance = (balance % 10);         if (dimes > 0) {             isDimesOn = true;         }                 //nickles         int nickles = balance / 5;         balance = (balance % 5);         if (nickles > 0){             isNicklesOn = true;         }                 //pennies         int pennies = balance / 1;         balance = (balance % 1);         if (pennies > 0){             isPenniesOn = true;         }                 //checking for difference over 0         if (difference < 0) {             JOptionPane.showMessageDialog(null, "The amount received can't be less than the amount owned");         } else {             // Returning Message             if (isFivtyOn) {                         System.out.println("Fivty dollar bill "+ fivtydollars);             } if (isTwentybillOn) {                         System.out.println("Twenty dollar bill " + twentydollars);             } if (isTenbillOn) {                         System.out.println("Ten dollar bill: " + tendollars);             } if (isDollarOn) {                         System.out.println("One Dollar Bill: " + dollars);             } if (isQuartersOn) {                         System.out.println("Quarters: " + quarters);             } if (isDimesOn) {                         System.out.println("Dimes: " + dimes);             } if (isNicklesOn) {                         System.out.println("Nickles: " + nickles);             } if (isPenniesOn) {                         System.out.println("Pennies: " + pennies);             } else {                 System.out.println("Thanks! have a good day");             }         }                                             }     }```
• 01-27-2014, 07:47 PM
KevinWorkman
Re: Problem with Cash return
This code does not have output. Can you post the code that does that?
• 01-27-2014, 07:54 PM
outime
Re: Problem with Cash return
Quote:

Originally Posted by KevinWorkman
This code does not have output. Can you post the code that does that?

• 01-27-2014, 08:33 PM
KevinWorkman
Re: Problem with Cash return
You've run into a problem that many people have experienced: floating types (including double) do not have infinite accuracy. This means that 50.01 - 19.51 won't equal exactly 30.5, which throws off your entire code. To test my theory, add this line:

//calculate difference
double difference = (amountr - amountd);

System.out.println(amountr + " - " + amountd + " = " + (amountr-amountd));

And you'll see that it equals 30.4999999999999

For more information, google "what every computer scientist should know about floating point arithmetic", but the gist is this: don't store things like money in the form of a double or a float.
• 01-27-2014, 08:58 PM
gimbal2
Re: Problem with Cash return
... but in stead store it as cents.

Or apply a universal rule: round everything the same way EVERYWHERE and thus don't have infinite precision by choice. At the company I work for, everything is calculated as BigDecimal but in the end rounded to 2 digits, half up. That works, but every once in a while we have to hunt for a "1 cent difference" bug because the rounding is not done properly somewhere. If your code does thousands of calculations, that is pretty bad.
• 01-27-2014, 08:59 PM
outime
Re: Problem with Cash return
Quote:

Originally Posted by KevinWorkman
You've run into a problem that many people have experienced: floating types (including double) do not have infinite accuracy. This means that 50.01 - 19.51 won't equal exactly 30.5, which throws off your entire code. To test my theory, add this line:

//calculate difference
double difference = (amountr - amountd);

System.out.println(amountr + " - " + amountd + " = " + (amountr-amountd));

And you'll see that it equals 30.4999999999999

For more information, google "what every computer scientist should know about floating point arithmetic", but the gist is this: don't store things like money in the form of a double or a float.

And what will be the solution? If I want to get amounts type "57.5" etc
• 01-27-2014, 09:03 PM
KevinWorkman
Re: Problem with Cash return
Quote:

Originally Posted by outime
And what will be the solution? If I want to get amounts type "57.5" etc

Check out Gimbal's reply. Either use BigDecimal or store them as ints.
• 01-27-2014, 09:40 PM
outime
Re: Problem with Cash return
Quote:

Originally Posted by KevinWorkman
Check out Gimbal's reply. Either use BigDecimal or store them as ints.

But that's exactly what I'm doing here

Code:

```//convert to pennies         int balance = (int)(difference * 100);```
• 01-27-2014, 09:54 PM
KevinWorkman
Re: Problem with Cash return
Quote:

Originally Posted by outime
But that's exactly what I'm doing here

Code:

```//convert to pennies         int balance = (int)(difference * 100);```

Right, but the problem has already occurred by that point. You need to store difference itself as an int or a BigDecimal.
• 01-28-2014, 12:50 AM
AlexGraal
Re: Problem with Cash return
AKA, the amounts that the people enter need to be in cents. Whether you ask them to put it in as cents, or you do

Code:

```System.out.println("Enter whatever:"); int money1 = (int) scan.nextDouble() * 100; System.out.println("Enter whatever:"); int money2 = (int) scan.nextDobule() * 100;```
And then use those ints to do the math.

Code:

`int difference = money1 - money2;`