How to format a floating number to fixed width in Python

后端 未结 8 1118
长发绾君心
长发绾君心 2020-11-22 10:26

How do I format a floating number to a fixed width with the following requirements:

  1. Leading zero if n < 1
  2. Add trailing decimal zero(s) to fill up f
8条回答
  •  我在风中等你
    2020-11-22 10:53

    In python3 the following works:

    >>> v=10.4
    >>> print('% 6.2f' % v)
      10.40
    >>> print('% 12.1f' % v)
            10.4
    >>> print('%012.1f' % v)
    0000000010.4
    

提交回复
热议问题