Results 1 to 8 of 8
  1. #1
    Intermission is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Question about my code (double, int)

    Well the basic of my program would be to insert coins (pennies,dimes, nickles, and quarters) then print out the total in $ format.

    The problem is when ever I tied added some number like 3 nickels to the bank (total) then print out the total (getContents), I would get result such as 0.15000000000000002 instead of just 0.15(3*0.05). Another example would be adding 24 dimes to the bank. Instead of getting 2.4 (24*0.10), I would get 2.4000000000000004
    from the getContents method instead.

    I'm i doing anything wrong? Is there a way around this? I can't seem to find a way to fix it. I think it has to do with double but I am not sure what it is. Thanks for your help.

    Here are my codes.

    Java Code:
    public class PiggyBank
    {
    private int pennies;
    private int nickels;
    private int dimes;
    private int quarters;
    private double total;
     
    public PiggyBank()
    {
        pennies = 0;
        dimes = 0;
        quarters = 0;
        nickels = 0;
        total = 0.0;
    
    }
    public void addPennies(int p)
    {    
        if(p>=0){
            pennies = pennies + p;
            total = total + p*0.01;
         }
        else {
            System.out.println("Please use the correct amount");
        }  
    }
    public void addNickels(int n)
    {
        if(n>=0){
            nickels = nickels + n;
            total = total + n*0.05;
        }
        else {
            System.out.println("Please use the correct amount");
        }  
    }  
    public void addDimes(int d) // Add dimes.
    {
        if(d>=0){
            dimes = dimes + d;
            total = total + d*0.10;
        }
        else {
            System.out.println("Please use the correct amount");
        }  
    }
    public void addQuarters(int q)
    {
        if(q>=0){
            quarters = quarters + q;
            total = total + q*0.25;
        }
        else {
            System.out.println("Please use the correct amount");
        }  
    }
    
    public void getContents()
    {
        System.out.println("The bank has");
        System.out.println("________________________");
        System.out.println("pennies  = " + pennies);
        System.out.println("nickels  = " + nickels);
        System.out.println("dimes    = " + dimes);
        System.out.println("quarters = " + quarters);
        System.out.println("________________________");
        System.out.println("Total of $ " + total);
    }
    public void breakTheBank() // set money to 0 
    {
    System.out.println("Broke the bank and got $"  + total + " from it");
        pennies = 0;
        dimes = 0;
        quarters = 0;
        nickels = 0;
        total = 0.0;
    System.out.println("Bank has $" + total + " in it");
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Intermission is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    If i uses
    Java Code:
    private int total;
    I would get errors for my total lines
    Java Code:
    total = total + p*0.01;
    error: possible lose of precision.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Not if total is based in cents and not in dollars. Then the equation is
    Java Code:
    total += p;
    Then divide by 100 only when you want to display dollars, but remember to format the output so it looks nice either with a DecimalFormatter or a String.format(...)

  5. #5
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    157
    Rep Power
    5

    Default use round in java.lang.Math

    use the round function
    but i also surprise if you give
    new PiggyBank().addNickels(5);
    the o/p be--0.25
    if
    new PiggyBank().addNickels(3);
    op--Total of $ 0.15000000000000002
    i think because of multiplying an int by double cause this problem


    Math (Java 2 Platform SE v1.4.2)

  6. #6
    Intermission is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks, I got it to work now with
    Java Code:
    total += p;
    total += d*5;
    total += q*25;
    total += n*10;
    
    System.out.println(total/100);

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Where's that link about "what every programmer needs to know about floats" when I need it?

    Ah...here we go.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Where's that link about "what every programmer needs to know about floats" when I need it?

    Ah...here we go.
    An excellent article; a very simple explanation is that a double type can't represent certain numbers exactly (analogy, 1/3 can't be exactly represented using decimal fractions). Suppose you have 52 cards, numbered 1/2, 1/4, 1/8 ... 1/2^52. One of each. Pick any combination of cards such that the sum of the cards equals 1/10. You can't do it and neither can I. So 1/10 (0.1 decimal) can't be exactly represented by a double type number; any trickery dickery is doomed to fail. All you can do is either represent (and implicitly round) those numbers the way you want; Formatter classes are built for this purpose; or you can change everything to the integer domain. e.g. use cents as a monetary unit instead of dollars.

    kind regards,

    Jos

Similar Threads

  1. Quick question about this simple code..
    By shroomiin in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 05:58 PM
  2. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  3. Replies: 1
    Last Post: 10-02-2008, 03:06 AM
  4. Replies: 1
    Last Post: 08-29-2008, 01:54 PM
  5. need a java code for this question!
    By rose in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 07:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •