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?


Try looking at the output of this instead

	double totalCost = 3.91, amountPaid = 4.00;
        double totalChange;


totalChange = ((amountPaid - totalCost) * 100); System.out.println(totalChange);

You'll see it isn't 9, and the cast to int truncates it to 8, as you were seeing originally.

The problem is that you can't use floating point arithmetic (doubles) to do precise arithmetic. If possible then you should try using fixed point arithmetic. e.g.

	int totalCost = 391, amountPaid = 400;
	int totalChange;

	totalChange = amountPaid - totalCost;
	System.out.println(totalChange);


James .



Relevant Pages

  • Re: Finally back behind the kit
    ... it allows the drummer to play doubles with one foot and still keep the left foot for hi-hat or whatever else s/he can do with it. ... My guess is that I'll need to be in a cast for at least another two weeks after that, maybe another month (12 - 14 weeks total downtime. ...
    (rec.music.makers.percussion)
  • Floating Point Accuracy Issue
    ... Im a little rusty regarding floating point accuracy, ... by a maximum tolerance of 3.5, rounded up and then cast back to unsigned ... cast to a DWORD cycles the result back to 0 as its 1 number higher than can ... const double dMaxTol = 3.5; ...
    (microsoft.public.vc.language)
  • Re: Error in CF Decimal-type calculations?
    ... I was able to recreate the issue and the only workaround I found was to ... problem using floating point types or to cast to double before using the ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: POLLING
    ... the bread they have cast on the waters comes floating back to them, ...
    (alt.true-crime)
  • Re: gcc math
    ... if you cast a floating ... > point number to an integer, all the digits after the decimal point are ... tauno voipio iki fi ...
    (comp.os.linux.development.apps)