Re: sorry .09 instead of .08



joshdalrymple2000@xxxxxxxxx wrote:
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?

Never ever perform money calculation using floating point.
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)
.




Relevant Pages

  • Re: Would I consider sending it to Norway?
    ... made to enter what currency or currencies they are willing to receive. ... If EBay made it POSSIBLE to specify this then I might agree. ... also need to allow you to specify the charges for such payment. ... extremely expensive to receive non sterling payments, for example, so I ...
    (uk.people.consumers.ebay)
  • Re: specify the devimal separator of a currency
    ... Each new year I create a new table with currency ... if you create a new database form MS Access you can specify the number of ... decimals after the dot of a currency field. ...
    (borland.public.delphi.database.ado)
  • Re: Range Validation Problem
    ... You need to also specify the type of the data you're validating, ... string, integer, double, date or currency. ... > verified the Min/Max values in the Range Validator. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)