Jane Doe2
Jane Doe2

Reputation: 141

Program to get change (rounding help)

I am trying to round the number of nickels in my program (there are no pennies anymore). So if I input 1.44 it should round to 1.40 and if I had 1.46 it should round to 1.50. Please help!

import java.util.Scanner;

public class MakingChange
{

    private static Scanner scanner;

    public static void main(String[] args)

    {
        scanner = new Scanner(System.in);
        double amount = 0;
        while (true) {
             try {
                 amount = Double.parseDouble(scanner.nextLine());
                 break; // will only get to here if input was a double
             } catch (NumberFormatException ignore)   {
                 System.out.println("INVALID\r\n$");
             }
         }

        //calculating amount of change in cents 
        int remainingAmount = (int)(amount * 100.00);
        //toonies
        int numberofToonies =   (int) (remainingAmount / 200.00);
        remainingAmount = remainingAmount % 200;
        //loonies   
        int numberofLoonies = (int) (remainingAmount / 100.00);
        remainingAmount = remainingAmount % 100;
        //quarters
        int numberofQuarters = (int)(remainingAmount / 25.00);
        remainingAmount = remainingAmount % 25;
        //dimes      
        int numberofDimes = (int)(remainingAmount / 10.00);
        remainingAmount = remainingAmount % 10;
        //nickels  
        int numberofNickels = (int)(remainingAmount / 5.00);
        remainingAmount = remainingAmount % 5;
        //rounded value
       numberofNickels=(int) (((amount -(numberofToonies * 2) - (numberofLoonies *1) - (numberofQuarters *0.25) - (numberofDimes * 0.10) - (numberofNickels * 0.05))+0.04)/0.05);

System.out.println(".*toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels +"$" );


        }

}

Upvotes: 0

Views: 199

Answers (2)

Svetlana
Svetlana

Reputation: 26

Do not forget to ask user for his input

public class MakingChange
{
    private static Scanner scanner;

    public static void main(String[] args) {

        scanner = new Scanner(System.in);
        System.out.println("Insert amount:");
        double amount = 0;
        while (true) {
            try {
                amount = Double.parseDouble(scanner.nextLine());
                break; // will only get to here if input was a double
            } catch (NumberFormatException ignore) {
                System.out.println("INVALID\r\n$");
            }
        }

        // calculating amount of change in cents
        int remainingAmount = (int) (amount * 100.00);
        // toonies
        int numberofToonies = (int) (remainingAmount / 200.00);
        remainingAmount = remainingAmount % 200;
        // loonies
        int numberofLoonies = (int) (remainingAmount / 100.00);
        remainingAmount = remainingAmount % 100;
        // quarters
        int numberofQuarters = (int) (remainingAmount / 25.00);
        remainingAmount = remainingAmount % 25;
        // dimes
        int numberofDimes = (int) (remainingAmount / 10.00);
        remainingAmount = remainingAmount % 10;

        // nickels
        int numberofNickels = 0;
        // if the remaining amount is exactly 5 cents
        if (remainingAmount == 5) {
            numberofNickels = 1;
        } else if (remainingAmount > 5)// round to higher value if remaining
                                        // value is greater than 5 cents e.g
                                        // 20.68
        {
            numberofDimes += 1;
        }

        System.out.println(".*toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:"
                + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels + "$");

    }
}

Upvotes: 0

Aaron
Aaron

Reputation: 24812

I would suggest to multiply the number by ten, round it with Math.round(), then divide it back by ten.

1.46 * 10 = 14.6
Math.round(14.6) = 15
15 / 10 = 1.5

1.44 * 10 = 14.4
Math.round(14.4) = 14
14 / 10 = 1.4

This can be implemented by the following lambda :

d -> Math.round(d * 10) / 10.0

Specifying 10.0 instead of 10 is important since Math.round() returns a long value and you don't want integer divison but float division.

You can see it in action on this ideone snippet.

Upvotes: 2

Related Questions