Sorry for the unspecific title but I'm not sure how to describe this. This code is for calculating how many bills in different denominations will be withdrawn from an ATM based on a user input number.
Everything works perfectly but when I enter anything with 0.X9 at the end it will output cents to 0.X8 instead of 0.X9. For example, I enter 245.89 as the original number and at the end of the cents calculation, it will output 88 cents instead of 89. However, with any other number 0-8 it will work perfectly.
Any advice will be greatly appreciated!
import java.util.Scanner;
public class ATM { public static void main(String[] args) {
System.out.print("Please enter USD withdrawal amount: ");
Scanner user = new Scanner(System.in);
double usdTotal = user.nextDouble();
double billsOut;
double bills20 = (int)(usdTotal / 20 );
double bills10 = (int)(usdTotal % 20) / 10;
double remainder10 = (usdTotal % 20);
double bills5 = (int)((remainder10) - bills10 * 10) / 5;
double remainder5 = (remainder10 % 5);
double bills1 = (int)(remainder5);
double cents = (int)((usdTotal - (int)usdTotal) * 100);
System.out.println("Twenty Dollar Bills: " + bills20);
System.out.println("Ten Dollar Bills: " + bills10);
System.out.println("Five Dollar Bills: " + bills5);
System.out.println("One Dollar Bills: " + bills1);
System.out.println("Cents: " + cents);
System.out.println(usdTotal);
System.out.print((int)(usdTotal));
user.close();
}
}
> run ATM
Please enter USD withdrawal amount: [DrJava Input Box]
Twenty Dollar Bills: 38.0
Ten Dollar Bills: 1.0
Five Dollar Bills: 1.0
One Dollar Bills: 2.0
Cents: 78.0
777.79
777>