How to format a floating number to fixed width in Python

后端 未结 8 1109
长发绾君心
长发绾君心 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 11:05

    In Python 3.

    GPA = 2.5
    print(" %6.1f " % GPA)
    

    6.1f means after the dots 1 digits show if you print 2 digits after the dots you should only %6.2f such that %6.3f 3 digits print after the point.

提交回复
热议问题