Decimal arithmatic, was Re: Python GUI app to impress the boss?

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Sep 30 04:19:10 CEST 2002


----- Original Message ----- 
From: "Paul Rubin" <phr-n2002b at NOSPAMnightsong.com>


> "Chris Gonnerman" <chris.gonnerman at newcenturycomputers.net> writes:
> > >>> .70 * .05
> > 0.034999999999999996
> > 
> > Rounded to two decimals, that's 0.03; but done in proper
> > decimal numbers, the answer should be 0.035, rounded to 0.04
> > using either "classic" or "banker's" decimal rounding.
> > ...
> > So, Bengt, how do I do this right using only floats?  I can't.
> > I have to have decimal arithmetic to get the right answer.
> 
> How about just adding a small amount before rounding at the end?
> 
> >>> (.70 * .05) + .000000001
> 
> rounds to the right thing.

Cool.  Now PROVE that's right in all cases.

Like I said, decimal arithmetic is STILL the only choice.

Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net





More information about the Python-list mailing list