Skip to content

Instantly share code, notes, and snippets.

@kkdd
Last active March 14, 2017 13:46
Show Gist options
  • Save kkdd/a882838326f6abb9638a to your computer and use it in GitHub Desktop.
Save kkdd/a882838326f6abb9638a to your computer and use it in GitHub Desktop.
浮動小数点数のリストの表示出力 ref: http://qiita.com/kkdd/items/4f3c112ccb043d4bc501
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment