I'm attempting to write a method where the input is an amount of money and it returns the minimum amount of pennies needed for that dollar amount (ex. 1.11 would have one penny). My code works for smaller numbers for breaks with bigger numbers.
public static void main (String[] args) {
int result = (lessPennies(200.0));
System.out.println(result);
}
public static int lessPennies(double n) {
double pennies = n % .05;
pennies = (pennies * 100);
pennies = Math.round(pennies);
int penniesReturn = (int) pennies;
return penniesReturn;
}
I'm expecting the output here for pennies to be zero, given that 200 / .05 is 4000 and should not have anything left over, yet I get 5 as an answer. I understand why this happens but I'm not sure how to fix it and not break the code for smaller inputs.