Floating Number format problem
Marc Christiansen
usenet at solar-empire.de
Tue Jun 12 07:05:27 EDT 2007
Peter Otten <__peter__ at web.de> wrote:
> Marc Christiansen wrote:
>
>> Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
>>> En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you at gmail.com> escribió:
>>>
>>>> On 6 12 , 3 16 , ici <iltch... at gmail.com> wrote:
>>>>> On Jun 12, 10:10 am, <kelvin.... at gmail.com> wrote:
>>>>>
>>>>> > How could I format the float number like this: (keep 2 digit
>>>>> > precision)
>>>>> > 1.002 => 1
>>>>> > 1.12 => 1.12
>>>>> >>> print "%.02f" % (2324.012)
>>>>> 2324.01
>> Or:
>>
>> def my_other_formatter_ommitting_trailing_zeroes(value):
>> result = '%.2f' % value
>> return result.rstrip('0.')
>
> Make that result.rstrip("0").rstrip("."), or it may fail:
>
>>>> ("%.2f" % 100.0).rstrip(".0")
> '1' # wrong
>>>> ("%.2f" % 100.0).rstrip("0").rstrip(".")
> '100'
Oops, didn't think of what happens if the value is a multiple of 10.
Thanks for correcting me. And thanks to John, who found it too.
Marc
More information about the Python-list
mailing list