Display output of a list of floating point numbers

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

Display output of a list of floating point numbers
Display a list of alphabets in Python 3
String conversion of a list containing numbers
Display of fractions (list)
Convert a slice object to a list of index numbers
Display a list of frequently used commands on Zsh
How to display a list of installable versions with pyenv
Generate a list of consecutive characters
I tried to create a list of prime numbers with python
Introduction of ls command lsix that can display a list of images
Check the in-memory bytes of a floating point number float in Python
[python] Get a list of instance variables
[Python] Get a list of folders only
[Python] Concatenate a List containing numbers and write it to an output file.
[python] Create a list of various character types
Make a copy of the list in Python
Get a list of IAM users with Boto3
Get a list of Qiita likes by scraping
Output in the form of a python array
Floating point police 24:00
Make a list of latitude and longitude and convert UTM coordinates at once → File output
[Linux] A list of Linux commands that beginners should know
A memorandum about the warning of the pylint output result
A list of stumbling blocks in Django's image upload
Add a list of numpy library functions little by little --a
How to write a list / dictionary type of Python3
Make a note of the list of basic Pandas usage
How to convert floating point numbers to binary numbers in Python
A script that outputs a list of SoftLayer portal users
Floating point numbers are not the same as decimal numbers
Python: Get a list of methods for an object
Super simple: A collection of shells that output dates
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
Output the output result of sklearn.metrics.classification_report as a CSV file