Results 1 to 8 of 8
- 02-22-2010, 05:56 AM #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"); } }
-
don't use doubles or floats but instead do all your calculations in ints representing cents.
- 02-22-2010, 06:06 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
If i uses
I would get errors for my total linesJava Code:private int total;
error: possible lose of precision.Java Code:total = total + p*0.01;
-
Not if total is based in cents and not in dollars. Then the equation is
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(...)Java Code:total += p;
- 02-22-2010, 06:25 AM #5
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)
- 02-22-2010, 06:31 AM #6
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);
- 02-22-2010, 12:53 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Where's that link about "what every programmer needs to know about floats" when I need it?
Ah...here we go.
- 02-22-2010, 01:24 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
-
Quick question about this simple code..
By shroomiin in forum New To JavaReplies: 2Last Post: 11-10-2009, 05:58 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM -
Simple Question Making a Variable Accessable throught the code
By 2o2 in forum New To JavaReplies: 1Last Post: 10-02-2008, 03:06 AM -
How to add or code for 'Question and Answer' on product page of website
By 82rathi.angara in forum Advanced JavaReplies: 1Last Post: 08-29-2008, 01:54 PM -
need a java code for this question!
By rose in forum New To JavaReplies: 3Last Post: 05-07-2008, 07:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks