decimal numbers
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Feb 15 21:30:13 EST 2014
On Sat, 15 Feb 2014 14:34:45 -0700, Ian Kelly wrote:
> 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'
Oh! That's much nicer than my solution. And it even works with %-style
string interpolation too!
py> "%g" % 23.0
'23'
py> "%g" % 23.1
'23.1'
Even though I didn't ask the question, I learned something from the
answer. Thank you.
--
Steven
More information about the Python-list
mailing list