0

I am creating a system for giving back change. When I run the program I get: (Lets say i gave $100)

You have given 100.0 dollars.
Your change is 14.670000000000002 dollars.
You will recieve:
0.6700000000000017 dollars
0.1700000000000017 quarters
0.0700000000000017 dimes
0.020000000000001697 nickels
1.6965595595053173E-15 pennies

code

import java.util.Scanner;
public class RunGivingChange {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        double given;
        double cost = 85.33;
        double change;
        int dollars;
        int quarters;
        int dimes;
        int nickels;
        int pennies;

        Scanner input = new Scanner(System.in);

        System.out.println("Your total is " + cost + " dollars.");
        System.out.println("How much money are you giving?");
        given = input.nextDouble();
        if (given >= cost)
        {
            System.out.println("You have given " + given + " dollars.");
            change = given - cost;
            System.out.println("Your change is " + change + " dollars.");
            System.out.println("You will recieve:");

            //dollars
            change = change % 1;
            System.out.println(change + " dollars");

            //quarters
            change = change % 0.25;
            System.out.println(change + " quarters");

            //dimes
            change = change % 0.10;
            System.out.println(change + " dimes");

            //nickels
            change = change % 0.05;
            System.out.println(change + " nickels");

            //pennies
            change = change % 0.01;
            System.out.println(change + " pennies");
        }
        else if (given < cost)
        {
            System.out.println("Not enough money.");
        }
        else
        {
            System.out.println("Error.");
        }

    }

    }
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Max Chapin
  • 35
  • 1
  • 1
  • 7
  • Recommendation/request for your next question at least: be as specific as you can. `It's giving the wrong number` doesn't help much. At least we'd need to know what you consider to be wrong: is it the precision, the result of your calculations, something else? – Thomas Sep 22 '15 at 12:18

4 Answers4

3

You're using double precision floating point values to represent money.

Don't do that.

Floating point numbers give you around 15 significant figures of accuracy, but with a phenomenal range (and computational performance) that makes them useful for scientific usage.

Used a fixed point type or BigDecimal for money values.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Yup, that's pragmatic, but don't forget currencies that have 1,000 "cents" to the "dollar", for example Tunisian Dinars. That can "eat" up quite a lot of your integer. You'd be OK with 64 bit but not 32. – Bathsheba Sep 22 '15 at 12:14
  • I woult use joda-money library instead of reinventing the wheel – Gaskoin Sep 22 '15 at 13:15
0

Modulo (%) doesn't work as you expect it to do (besides the precision problems):

2.67 % 1 won't result in 2 but in 0.67.

(for some more information have a look here: How do I use modulus for float/double?)

You'd probably want to calc Math.floor( 2.67 / 1) etc. and subtract the rounded value from change.

Alternatively, if change is always positive (should probably be) you can just cast (and thus cut) to an integer:

int dollars = (int)(change / 1);
change -= dollars;

int quarters = (int)(change / 0.25);
change -= 0.25 * quarters;

...

To overcome the precion issues either use BigDecimal or convert all values to pennies, i.e. change = 1467 pennies, 1 dollar = 100 pennies etc.

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
0

Instead of having the cost in dollars you could express it in the smallest division (pennies) :

double cost = 85.33;// dollars
you could express it as :
int cost = 8533: .. //pennies

Then you could use a / and % sequence of operations to compute each of the currency division. Ex:

int dollars = change / 100; // how many dollars fit in that pennies amount
change = change % 100; // how much pennies are left

//repeat for the other subdivisions
0

If you are using doubles, make sure to round to decimal places.

change = (double) Math.round(change *100) / 100;

Second, mod (%) returns the remainder of the statement

14.67 % 1.0 = 0.67

What you want to do first is find how many times 1.0 will divide into 14.

dollars = Math.floor(change / 1);
change = change % 1.0;

This will round down to the nearest integer, and will give you how many times the 1.0 can go into the change.

The problem with using a double here is like what Bathsheba said. To fix this, you will have to round after every time change is modified.

This should give you the correct outputs.

import java.util.Scanner;

public class RunGivingChange{


    public static void main(String[] args) {


        double given;
        double cost = 85.33;
        double change;
        int dollars;
        int quarters;
        int dimes;
        int nickels;
        int pennies;

        Scanner input = new Scanner(System.in);

        System.out.println("Your total is " + cost + " dollars.");
        System.out.println("How much money are you giving?");
        given = input.nextDouble();
        if (given >= cost) {
            System.out.println("You have given " + given + " dollars.");
            change = given - cost;
            // round the change
            change = (double) Math.round(change *100) / 100;
            System.out.println("Your change is " + change + " dollars.");
            System.out.println("You will recieve:");

            //dollars     

            System.out.println(Math.floor(change / 1) + " dollars");
            change = change % 1;
            change = (double) Math.round(change *100) / 100;
            //quarters

            System.out.println(Math.floor(change / 0.25) + " quarters");
            change = change % 0.25;
            change = (double) Math.round(change *100) / 100;
            //dimes

            System.out.println(Math.floor(change / 0.1) + " dimes");
            change = change % 0.10;
            change = (double) Math.round(change *100) / 100;
            //nickels

            System.out.println(Math.floor(change / 0.05) + " nickels");
            change = change % 0.05;
            change = (double) Math.round(change *100) / 100;
            //pennies

            System.out.println(Math.floor(change / 0.01) + " pennies");

        } else if (given < cost) {
            System.out.println("Not enough money.");
        } else {
            System.out.println("Error.");
        }

    }

}

There are better ways to do this, next time you should search the site. There are multiple answers for this topic.

How to convert money to change

Compute the different ways to make (money) change from $167.37?

Show money in change

Community
  • 1
  • 1
Eric G
  • 928
  • 1
  • 9
  • 29