[Tutor] cut all decimal places with zeros
Tim Michelsen
timmichelsen at gmx-topmail.de
Wed Apr 16 01:20:07 CEST 2008
>> how can I suppress the decimal places for (only those) numbers whos
>> decimal places are zero (0)?
>
> I don't know how to do this with just string formatting but I think
> ('%.4f' % n).rstrip('.0')
> will do what you want.
No.
I tested with
n = 10.0
You code returns '1'
My code returns '10'
Here again:
### CODE ###
def cut_decimals(float):
"""
input: floating number
output: number as string with zero decimals removed
"""
#print float-int(float)
#print '%.4f' %float
if float-int(float) != 0:
number = '%.4f' %float
number = number.replace('0.','X.')
number = number.replace('0','')
number = number.replace('X.','0.')
else:
number = '%.0f' %float
return number
#### END ###
Regards,
Timmie
More information about the Tutor
mailing list