# Thread: Question about my code (double, int)

1. Member
Join Date
Feb 2010
Posts
3
Rep Power
0

## 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. Member
Join Date
Feb 2010
Posts
3
Rep Power
0
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.

3. 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(...)

4. ## 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)

5. Member
Join Date
Feb 2010
Posts
3
Rep Power
0
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);```

6. Moderator
Join Date
Apr 2009
Posts
11,820
Rep Power
19
Where's that link about "what every programmer needs to know about floats" when I need it?

Ah...here we go.

7. Originally Posted by Tolls
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

#### Posting Permissions

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