Search code examples
javatype-conversionroundingfile-conversion

Java Money Denomination Rounding Issues


So I was assigned to write a program for my programming class that prompts the user and reads the entered value as a double (maximum input is [99.99] ninety-nine dollars, and ninety-nine cents), which will represent a monetary amount. I'm then to determine the fewest number of each bill and coin needed to represent said amount (10 dollar bill is the maximum size needed).

I'm having issues with rounding. For example, an input of 99.99 will return 3 pennies, when it should be 4 pennies. I was explicitly told by my teacher to not go beyond the material covered in class (only have gone as far as creating scanners, converting data types, and casting. Which means no BigDecimal). Any help would be appreciated.

import java.util.Scanner;

public class MoneyConversion
{
     public static void main(String[] args)
     {
          //Set variables for input, dollars, and cents
          double inputAmount, change;
          int tens, fives, ones;
          int quarters, dimes, nickels, pennies;

          //Create scanner to setup for user input
          Scanner scan = new Scanner(System.in);

          //Scan user input and store into double
          System.out.print("Enter monetary amount: ");
          inputAmount = scan.nextDouble();

          //Extract demical or cent amount from dollar amount
          change = (inputAmount - (int) inputAmount) * 100;

          System.out.println("That's equivalent to:");

          //Extract and print denomination for ten-dollar bills
          tens = (int) inputAmount / 10;
          inputAmount = inputAmount % 10;
          System.out.println(tens + " ten dollar bills");

          //Extract and print denomination for five-dollar bills
          fives = (int) inputAmount / 5;
          inputAmount = inputAmount % 5;
          System.out.println(fives + " five dollar bills");

          //Extract and print denomination for one-dollar bills
          ones = (int) inputAmount;
          System.out.println(ones + " one dollar bills");

          //Extract and print denomination for quarters
          quarters = (int) change / 25;
          change = change % 25;
          System.out.println(quarters + " quarters");

          //Extract and print denomination for dimes
          dimes = (int) change / 10;
          change = change % 10;
          System.out.println(dimes + " dimes");

          //Extract and print denomination for nickels
          nickels = (int) change / 5;
          change = change % 5;
          System.out.println(nickels + " nickels");

          //Convert value for change to integer value and print for pennies
          pennies = (int) change;
          System.out.println(pennies + " pennies");

     }

}

Solution

  • You would be better off converting the numbers to integers (the amount of pennies) and calculating based on that, as that way you can avoid the type of floating point errors you are experiencing now (they are caused by the representation of floating point numbers as binary).

    If you step through using this (You need to hard code the input to use it) you can see the errors.

    The change amount sets as 98.99999999999949 because of rounding on line 20, so that is why a penny goes missing