Re: .98 cents instead of .99




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?

Try (int) (Math.round(100 * (amountPaid - totalCost)))

Or better use, store the amount as an int/long in pennies
or use BigDecimal to avoid floating point precision issues.

.



Relevant Pages

  • Re: Who Sez Secrets Cannot be Kept?
    ... The saving grace is that they're 'spot checks', they're set up to hit 'likely' targets and they're usually not that secret.. ... Not saying you're wrong.. ... no amount of training will protect you from a ton of steel coming at you out of nowhere.. ... this one is more a public safety issue..Penalties for DUI are not really deterrents, so I have no issues with the spot-check idea to keep people honest.. ...
    (rec.martial-arts)
  • Re: Interbase 2007 Developer Edition included with Delphi 2007 registrationinformation
    ... That is exactly what I am saying. ... Downloadable products may be accessed by looking up your order. ... Product SKU: HDE0007WWES190 ... Amount: 1,327.50AUD ...
    (borland.public.delphi.non-technical)
  • Re: How to generate a Windows style license key
    ... > A company that refuses to behave like a good cooperate citizen needs to be ... You are saying that should end. ... some positive constant and I is the amount of revenue a company is ... money - and you go to jail, for the rest of your life:) No thank you. ...
    (sci.crypt)
  • Re: Borg suggestion for APW
    ... store ... ... risk and he should do something differently to collect those *Heal* pots. ... Another thing to record, if possible, is the amount of time he spends ... at dl1 either scumming for!speed or waiting for stores to restock. ...
    (rec.games.roguelike.angband)
  • Re: how get calculated amount from form to underlaying table
    ... store the exchange rate information, including the date it was in effect. ... You could then use this to redo the calculation when necessary. ... > the underlaying table both the original amount and currency and the amount ... > Foreign currency amount entered by way of text box - no problems. ...
    (microsoft.public.access.forms)