# [Homework] Calculating Change

• February 6th, 2013, 07:06 PM
nKulo
[Homework] Calculating Change
The idea of this assignment is to simulate a customer buying goods from a store. The problem that I am having is that when I try and calculate how much change the customer should receive, my program treats \$20.99 as \$20 so when I try and convert the double into an integer it gives me 900 instead of 901. I have been working at this for a couple hours now and just can't seem to find where my error lies. Any suggestions or pointers to the right direction and I would be grateful.

I haven't finished my if statement or PrintWriter statements because I know my arithmetic is not working the way I want and need it to.

Here is my code:

Code :

```import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner;     public class store {   /** * @throws FileNotFoundException * @name * @purpose Calculate purchases * @date February 6th, 2013 */ public static void main(String[] args) throws FileNotFoundException { Scanner Keyboard = new Scanner(System.in); PrintWriter prw = new PrintWriter("outputPA3.txt");   double Beer = 1.95; double Chips = 1.79; double Coke = 3.99;   System.out.println("Please enter your name:"); String name = Keyboard.nextLine(); System.out.println("Welcome to Stop and Rob, " + name + "! Today we have the following items on sale: "); System.out.println("Beer: \$" + Beer); System.out.println("Chips: \$" + Chips); System.out.println("Coke: \$" + Coke); System.out.println(); System.out.println("Please place your order:"); System.out.println(); System.out.println("How many beers would you like today?"); int numBeer = Keyboard.nextInt(); System.out.println(numBeer + " beers"); System.out.println("How many bags of chips? "); int numChips = Keyboard.nextInt(); System.out.println(numChips + " bags of chips"); System.out.println("How many 12-packs of Coke would you like? "); int numCoke = Keyboard.nextInt(); System.out.println(numCoke + " packs of Coke");   double total = (Beer * numBeer) + (Chips * numChips) + (Coke * numCoke); System.out.printf("Your bill today is \$%4.2f", total);   System.out.println(); System.out.println(name + " enter the amount you will be paying: "); double payment = Keyboard.nextDouble(); System.out.println("The amount you entered is: \$" + payment);   if (payment >= total) { int rAmount = (int)((payment - total) * 100); int rFives = rAmount / 500; rAmount = rAmount % 500; int rDollars = rAmount / 100; rAmount = rAmount % 100; int rQuarters = rAmount / 25; rAmount = rAmount % 25; int rDimes = rAmount / 10; rAmount = rAmount % 10; int rNickels = rAmount / 5; rAmount = rAmount % 5; int rPennies = rAmount; System.out.println("Your change of " + rAmount + " consists of \n" + "\t" + rFives + " fives\n" + "\t" + rDollars + " dollars\n" + "\t" + rQuarters + " quarters\n" + "\t" + rDimes + " dimes\n" + "\t" + rNickels + " nickels\n" + "\t" + rPennies + " pennies\n"); }   } }```
• February 6th, 2013, 07:55 PM
Norm
Re: [Homework] Calculating Change
doubles have rounding errors and other problems and are not recommended for use with money calculations.
Try to do all the computations in pennies and only convert for displaying.