I'm writing a program that grabs 2 inputs from the user: 1) the price of an item 2) the amount payed. It will then return the difference of the two (change due) and it will then compute how many bills and coins would be given back. I'm having trouble when finding the decimal (coin values accurately):
int one, five, ten, twenty,qs, ds, ns, ps, rem ;
double price, pay,change;
String user;
Scanner input = new Scanner(System.in);
do {
System.out.print("Price: $");
price = input.nextDouble();
System.out.print("From: $");
pay = input.nextDouble();
change = pay - price;
twenty = (int) (change / 20.00);
rem = (int) (change % 20.00);
ten = (int) (rem / 10.00);
rem = (int) (rem % 10.00);
five = (int) (rem / 5.00);
rem = (int) (rem % 5.00);
one = (int) (rem);
//
qs = (int) rem / 25;
rem = (int) rem % 25;
ds = (int) rem / 10;
rem = (int) rem % 10;
ns = (int) rem / 5;
rem = (int) rem % 5;
ps = (int) rem;
System.out.println("Computed change: $" + change);
System.out.println(twenty + " x $20 bills, \n" + ten
+ " x $10 bills, \n" + five + " x $5 bills, \n" + one
+ " x $1 bills, \n" + qs + " x 25c coins \n" + ds
+ " x 10c coins \n" + ns + " x 5c coins \n" + ps
+ " x 1c coins \n");
System.out.println("Would you like to enter another "
+ "quantity? (Y or N):");
input.nextLine();
user = input.nextLine();
if (!user.equalsIgnoreCase("y")) {
System.out.println("Goodbye!");
}
} while (user.equalsIgnoreCase("y"));
input.close();
// System.exit(0);
I'm almost sure the problem lies in casting the value of the remainder as an int, which would discard any value after the decimal point. I've tried dividng and modulo operation with .25, .10, .5, .1 but that doesn't seem to work. There is something I'm not seeing.