J Warrior777
J Warrior777

Reputation: 51

Trouble with the converting units

I am having a hard time understanding how to convert amounts such as $1.17 into this output:

I need to use if statements which I can figure out but, the problem i am having is trying to get the change amounts to display correctly. Here is my code. I`m a visual learner so if you start me in the right direction that would be helpful.

import java.util.Scanner;

public class ComputeChange {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an amount in double, for example 11.56: " );
        double number = input.nextDouble();
        System.out.println("Your amount " + number + " consists of ");
        int Dollars = (int) (number);
        int Quarters = Dollars / 25;

        if (number == 1) {
                    System.out.print("1 dollar ");
        }
            else if (number > 1) {
                System.out.print((int)Dollars + " dollars ");
            }
            if (number == 0) {
                System.out.println("");
            }
            System.out.print( Quarters + " Quarters ");
    }
}

Upvotes: 1

Views: 81

Answers (2)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

A much simpler approach than using modulus is to calculate the units of measurement in top-down fashion (from the highest to the lowest) and keep deducting them from the total already converted into the lowest unit.

A lot of libraries use this approach with units of time as well i.e. converting a time span into hours, minutes and seconds. Here's the same approach for currency. I've added inline comments to explain the code as best as possible.

// Scan the amount
Scanner scanner = new Scanner(System.in);
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.close();

// convert into cents 
int cents = (int) (amount * 100);

// get dollars
int dollars = cents/100;
// cents left after dollars
cents = cents - dollars*100;

// get quarters
int quaters = cents/25;
// cents left after quarters
cents = cents - quaters*25;

// get dimes
int dimes = cents/10;
// cents left after dimes
cents = cents - dimes*10;

// get nickels
int nickels = cents/5;
// cents left after nickels
cents = cents - nickels*5;

// leftover cents are pennies
int pennies = cents;

Now, just build the output message with a StringBuilder as

StringBuilder msg = new StringBuilder("You have:");
if (dollars > 0) {
    msg.append(" ").append(dollars).append(" dollar").append(dollars > 1 ? "s" : "");
}
if (quaters > 0) {
    msg.append(" ").append(quaters).append(" quarter").append(quaters > 1 ? "s" : "");
}
if (dimes > 0) {
    msg.append(" ").append(dimes).append(" dime").append(dimes > 1 ? "s" : "");
}
if (nickels > 0) {
    msg.append(" ").append(nickels).append(" nickel").append(nickels > 1 ? "s" : "");
}
if (pennies > 0) {
    msg.append(" ").append(pennies).append(" pennie").append(pennies > 1 ? "s" : "");
}

System.out.println(msg);

Output :

Enter amount: 1.17
You have: 1 dollar 1 dime 1 nickel 2 pennies

Enter amount: 12.99
You have: 12 dollars 3 quarters 2 dimes 4 pennies

Upvotes: 1

ug_
ug_

Reputation: 11440

You will probably want to use the modulus operator %. It is used with 2 numbers and returns the remainder of a divided by b where a is the left hand assignment and b is the right hand represented like a%b.

Example:

11%2=1 explanation: 5*2 = 10, 11-10 = 1

.66%.25=.16 explanation: 2*.25 = .5, .66-.5=.16


Start with a simple problem where you only have 2 types of coins, say or $0.08.

double monies = .08;
int numNickles = (int)(monies/.05) = 1 // one nickle
monies = monies % .05; // or you can write as monies %=.05;
// monies value should now be .03
int numPennies = (int)(monies/.01) = 3 // three pennies

Upvotes: 2

Related Questions