String Formatting Operations for decimals.
lacrima.maxim at gmail.com
Thu Apr 1 11:08:57 CEST 2010
I need to format a decimal (floating point) number in the following
10 results in '10'
10.5 results in '10.5'
10.50 results in '10.5'
10.5678 results in 10.57
How can I achieve this using standard Python string formatting
Something like '%.2f' works almost as expected:
>>> '%.2f' % 10.5
>>> '%.2f' % 10.5678
>>> '%.2f' % 10
But I always need trailing zeros to be excluded, i.e. 10.5 should
result in '10.5' (not '10.50'), and 10 should result in '10' (not
So how can I do this?
Sorry for my English.
Thanks in advance.
More information about the Python-list