Last active
March 14, 2017 13:46
-
-
Save kkdd/a882838326f6abb9638a to your computer and use it in GitHub Desktop.
浮動小数点数のリストの表示出力 ref: http://qiita.com/kkdd/items/4f3c112ccb043d4bc501
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(prettyfloat([1./3, 2./3], ndec=3)) # print pretty float numbers | |
# ==> [0.333, 0.667] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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