LoginSignup
1
1

More than 5 years have passed since last update.

浮動小数点数のリストの表示出力

Last updated at Posted at 2014-11-06

こんにちは。
浮動小数点数のリストを、見栄え良く print 表示出力(小数点以下の桁数を指定、float クラス の subclassing)。分数の表示(リスト)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)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1