PEP proposal for round(x,n) enhancement

Christopher Smith csmith at
Wed Sep 12 20:41:04 CEST 2001

python-list at writes:
>> exactly represent such numbers. Just try
>> >>> 1/5.
>> 0.20000000000000001
>The only differnce between Python and MATLAB here is that MATLAB doesn't
>displya as many digits as Python, so you don't see that 1 on the end.

Just a note here.  Python only does this when the repr() is called to 
display the number.  The str() function produces a more expected visual 

>>> x=1/5.
>>> print x
>>> print `x`
>>> x

Edu-sig had a way to make displayhook use str() when it prints numbers in 
the IDE environment so that 0.2 would have been printed when you just 
type a calculation (like 1/5. or a variable like x with no explicit 
print).  If you run the following code you can get this behavior:

import sys
import __builtin__
def mydisplayhook(o):
	if type(o) == type(0.1):
		# print a better looking float
		print str(o)
		# remember to assign to _!
		__builtin__._ = o
		#use the old displayhook in every other case

(Again, this is not the reason for the proposal for the round() 
enhancement but this might be useful.)


More information about the Python-list mailing list