Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cash to Change in Java

I am new to Java and was trying to write code that would tell you how much change you would receive for a given amount of money (in minimum number of coins). For example $0.31 would give 1 quarters, 0 dimes, 1 nickels, 1 pennies.

I got to a point where my code seemed to work. However, while most values work, specifically any multiple of 0.41 doesn’t work (ex. 0.41, 0.82 …). For example $0.41 results in 1 quarters,
1 dimes, 1 nickels, 0 pennies
, which only adds up to $0.40.

Here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.Scanner;

public class Cash {
    public static void main(String[] args){
        Scanner scanner_obj = new Scanner(System.in);

        System.out.print("Enter Amount: $");
        double amount = Double.parseDouble(scanner_obj.nextLine());
        double[] change = calc_Change(amount);

        System.out.println(change[0] + " Quarters");
        System.out.println(change[1] + " Dimes");
        System.out.println(change[2] + " Nickels");
        System.out.println(change[3] + " Pennies");

        System.out.println(check_Change(amount, change));
    }

    public static double[] calc_Change(double amount){
        double[] change = {0, 0, 0, 0};

        while (amount >= 0.25){amount -= 0.25; change[0] += 1;}
        while (amount >= 0.10){amount -= 0.10; change[1] += 1;}
        while (amount >= 0.05){amount -= 0.05; change[2] += 1;}
        while (amount >= 0.01){amount -= 0.01; change[3] += 1;}

        return change;
    }

    public static boolean check_Change(double amount, double[] change){
        double total = change[0]*0.25 + change[1]*0.10 + change[2]*0.05 + change[3]*0.01;
        System.out.println(amount + " vs " + total);

        return (amount == total);
    }
}

I was using the check_Change function to check my results.

I don’t know why this doesn’t work and why it is only certain numbers.

>Solution :

When you are subtracting from a double – the internal representation is not exact – running your code and printing out the amount every time you subtract yields the following:

0.41
0.15999999999999998
0.05999999999999997
0.009999999999999967

As you can see the binary representation of the double is not exact…

try convert to an int (i.e. multiply by 100) then do the calculations

public class CashMain {

    public static void main(String[] args){
        Scanner scanner_obj = new Scanner(System.in);

        System.out.print("Enter Amount: $");
        double amount = Double.parseDouble(scanner_obj.nextLine());
        System.out.println(amount);
        int numCents = (int) (amount*100);
        System.out.println(numCents);
        
        int[] change = calc_Change(numCents);

        System.out.println(change[0] + " Quarters");
        System.out.println(change[1] + " Dimes");
        System.out.println(change[2] + " Nickels");
        System.out.println(change[3] + " Pennies");

        System.out.println(check_Change(numCents, change));
    }

    public static int[] calc_Change(int amount){
        int[] change = {0, 0, 0, 0};

        while (amount >= 25){
            amount -= 25; 
            change[0] += 1;
        }
        while (amount >= 10){
            amount -= 10; 
            change[1] += 1;
        }
        while (amount >= 5){
            amount -= 5; 
            change[2] += 1;
        }
        while (amount >= 1){
            amount -= 1; 
            change[3] += 1;
        }

        return change;
    }

    public static boolean check_Change(int amount, int[] change){
        int total = change[0]*25 + change[1]*10 + change[2]*5 + change[3]*1;
        System.out.println(amount + " vs " + total);

        return (amount == total);
    }
}

output

Enter Amount: $0.41
0.41
41
1 Quarters
1 Dimes
1 Nickels
1 Pennies
41 vs 41
true
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading