How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
1.00 =1
1.567 =1.57
2324.012 =2324.01
>
I can not find any Formatting Operations is able to meet my
requirement.
Any suggestion will be appreciated.
En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you@gma il.comescribió :
>
>On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
>>On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
>>>
>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
>>>
>But in these case:
>>
>>>>print '%.02f'%1.002
>1.00
>>>>print '%.02f'%1.00
>1.00
>>
>I just expect it to output "1" , but these way will output 1.00
>
def my_formatter_om mitting_trailin g_zeroes(value) :
result = '%.2f' % value
if result[-3:]=='.00': result = result[:-3]
return result
>
for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
print "%g -%s" % (f, my_formatter_om mitting_trailin g_zeroes(f))
Or:
def my_other_format ter_ommitting_t railing_zeroes( value):
result = '%.2f' % value
return result.rstrip(' 0.')
Comment