Hello. Print a nice list of floating point numbers (specify the number of decimal places, subclassing the float class). Same as Display of fractions (list) prettyfrac ().
print(prettyfloat([1./3, 2./3], ndec=3)) # print pretty float numbers
# ==> [0.333, 0.667]
# ndec: a number of decimal places
def prettyfloat(x, ndec=2):
def pfform(x):
return ("%0." + str(ndec) + "f") % x
pfclas = type('', (float,), {'__repr__': pfform, '__str__': pfform})
def pf(x):
if isinstance(x, float):
return pfclas(x)
else:
return x
return map_recur(pf, x)
Recommended Posts