decimal numbers

Ian Kelly ian.g.kelly at gmail.com
Sat Feb 15 16:34:45 EST 2014


On Sat, Feb 15, 2014 at 11:57 AM, Luke Geelen <luke.geelen at gmail.com> wrote:
> hey, is it possible to remove the .0 if it is a valua without something behind the poit (like 5.0 gets 5 but 9.9 stays 9.9

The ':g' format specifier will trim off trailing zeroes, e.g.:

>>> '{:g}'.format(5.0)
'5'

It also switches to exponential notation if the scale of the number is
greater than the specified precision (default 6):

>>> '{:g}'.format(5000000.0)
'5e+06'

You can read up on string formatting for more details:
http://docs.python.org/3/library/string.html#formatstrings



More information about the Python-list mailing list