Format a number containing a decimal point with leading zeroes

Is that what you look for?

>>> "%07.1f" % 2.11
'00002.1'

So according to your comment, I can come up with this one (although not as elegant anymore):

>>> fmt = lambda x : "%04d" % x + str(x%1)[1:]
>>> fmt(3.1)
0003.1
>>> fmt(3.158)
0003.158

Like this?

>>> '%#05.1f' % 3.3
'003.3'

I like the new style of formatting.

loop = 2
pause = 2
print 'Begin Loop {0}, {1:06.2f} Seconds Pause'.format(loop, pause)
>>>Begin Loop 2, 0002.1 Seconds Pause

In {1:06.2f}:

  • 1 is the place holder for variable pause
  • 0 indicates to pad with leading zeros
  • 6 total number of characters including the decimal point
  • 2 the precision
  • f converts integers to floats