# Question about my code (double, int)

• 02-22-2010, 06:56 AM
Intermission
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

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.

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"); } }```
• 02-22-2010, 07:00 AM
Fubarable
don't use doubles or floats but instead do all your calculations in ints representing cents.
• 02-22-2010, 07:06 AM
Intermission
If i uses
Code:

`private int total;`
I would get errors for my total lines
Code:

`total = total + p*0.01;`
error: possible lose of precision.
• 02-22-2010, 07:15 AM
Fubarable
Not if total is based in cents and not in dollars. Then the equation is
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(...)
• 02-22-2010, 07:25 AM
javastuden
use round in java.lang.Math
use the round function
but i also surprise if you give
the o/p be--0.25
if
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)
• 02-22-2010, 07:31 AM
Intermission
Thanks, I got it to work now with
Code:

```total += p; total += d*5; total += q*25; total += n*10; System.out.println(total/100);```
• 02-22-2010, 01:53 PM
Tolls

Ah...here we go.
• 02-22-2010, 02:24 PM
JosAH
Quote:

Originally Posted by Tolls