Re: sorry .09 instead of .08
- From: Fabien Bergeret <fabien.bergeret@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jan 2006 11:36:10 +0100
joshdalrymple2000@xxxxxxxxx wrote:
Never ever perform money calculation using floating point.double totalCost = 3.91, amountPaid = 4.00; int totalChange;
totalChange = (int)((amountPaid - totalCost) * 100); System.out.println(totalChange);
it keeps saying the answer is 8... can any one help?
First solution : use BigDecimal and BigInteger (which are slower, as mentioned before).
Second solution (used in all our banking applications) : specify the amounts in "smallest currency unit" (cents for $, pence for pounds), and associate a "small unit number" with each currency (if you need to manage several currencies), which specifies the number of small units needed for GUI.
For instance, for $, calculations are made in cents, and display is done with amount/100 $ and amount%100 cents (small unit number = 2)
.
- Follow-Ups:
- Re: sorry .09 instead of .08
- From: Thomas Hawtin
- Re: sorry .09 instead of .08
- References:
- sorry .09 instead of .08
- From: joshdalrymple2000
- sorry .09 instead of .08
- Prev by Date: Re: JAR file which needs to call another jar file....
- Next by Date: Re: applet talking to server (with ajax?)
- Previous by thread: Re: sorry .09 instead of .08
- Next by thread: Re: sorry .09 instead of .08
- Index(es):
Relevant Pages
|
|