[Python-Dev] Rounding float to int directly (Re: struct module and coercing floats to integers)

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Aug 2 03:02:49 CEST 2006


M.-A. Lemburg wrote:

> You often have a need for controlled rounding when doing
> financial calculations 

You should NOT be using binary floats for money
in the first place.

 > or in situations where you want to
 > compare two floats with a given accuracy,

Pseudo-rounding to decimal places is not
the right way to do that. The right way is
to compare the difference to a tolerance.

> e.g. to work
> around rounding problems ;-)

As Tim Peters has pointed out many times,
you can't fix binary/decimal representation
problems by rounding. There are always cases
that give a different result than you would
want.

> Hmm, looks like a YAGNI to me,

In all my programming so far I've found
numerous uses for round-to-int, and exactly
zero uses for round-binary-float-to-decimal-
places. So from my point of view, the YAGNI
applies to the *current* behavour of round()!

--
Greg


More information about the Python-Dev mailing list