Hi Maxim, If it's the trailing zeroes you're concerned about, here's a work-around:
>>> ('%.2f' % 10.5678).rstrip('0') '10.57 I'm sure there are better solutions. But this one works for your need, right? Cheers,' Ching-Yun Xavier Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: cont...@xavierho.com Website: http://xavierho.com/ On Thu, Apr 1, 2010 at 7:08 PM, Lacrima <lacrima.ma...@gmail.com> wrote: > Hello! > > I need to format a decimal (floating point) number in the following > way: > 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 > operations? > Something like '%.2f' works almost as expected: > >>> '%.2f' % 10.5 > '10.50' > >>> '%.2f' % 10.5678 > '10.57' > >>> '%.2f' % 10 > '10.00' > 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 > '10.00'). > So how can I do this? > Sorry for my English. > Thanks in advance. > > with regards, > Maxim > -- > http://mail.python.org/mailman/listinfo/python-list >
-- http://mail.python.org/mailman/listinfo/python-list