Peter Otten <[EMAIL PROTECTED]> wrote: > Marc Christiansen wrote: > >> Gabriel Genellina <[EMAIL PROTECTED]> wrote: >>> En Tue, 12 Jun 2007 05:46:25 -0300, <[EMAIL PROTECTED]> escribió: >>> >>>> On 6 12 , 3 16 , ici <[EMAIL PROTECTED]> wrote: >>>>> On Jun 12, 10:10 am, <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list