Most Pythonic way to print *at most* some number of decimal places [duplicate]

The g formatter limits the output to n significant digits, dropping trailing zeroes:

>>> "{:.3g}".format(1.234)
'1.23'
>>> "{:.3g}".format(1.2)
'1.2'
>>> "{:.3g}".format(1)
'1'

Leave a Comment