set decimal place

Peter Hansen peter at engcorp.com
Thu Dec 27 22:55:53 EST 2001


Chris Barker wrote:
> 
> Peter Hansen wrote:
> 
> > >>> 10.0/3
> > 3.3333333333333335
> > >>> round(10.0/3, 4)
> > 3.3332999999999999
> > ^^^^^^^
> > This doesn't really solve the OP's problem.
> 
> > The concept of "having 4 decimal places" is meaningless with
> > floating point representations until you actually display the
> > value as a formatted string.
> 
> not completely. you might want to check if two values are the same to 4
> decimal places, in which case round should work just fine:
> 
> >>> 10.0/3 == 10.00001/3
> 0
> >>> round(10.0/3,4) == round(10.00001/3,4)
> 1

I don't know if this is guaranteed to work in all cases.
Comparing floating point values directly is generally 
not a good idea, although I don't know whether this applies
to rounded values, and if so how far you have to round them
before it makes it safe.

If it is safe, you have a valid point, but not likely what
the OP wanted.

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list