Format a number containing a decimal point with leading zeroes

前端 未结 5 1332
南旧
南旧 2021-02-20 04:48

I want to format a number with a decimal point in it with leading zeros.

This

>>> \'3.3\'.zfill(5)
003.3

considers all the dig

5条回答
  •  心在旅途
    2021-02-20 04:57

    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

提交回复
热议问题