Print floating point values without leading zero

后端 未结 13 1181
逝去的感伤
逝去的感伤 2020-11-30 05:21

Trying to use a format specifier to print a float that will be less than 1 without the leading zero. I came up with a bit of a hack but I assume there is a way to just drop

13条回答
  •  萌比男神i
    2020-11-30 06:03

    For Python 3

    When you want something simple and don't need negative number support:

    f'{k:.4f}'.lstrip('0')
    

    There are other solutions when you need negative number support, including the excellent regex by @nettux443.

提交回复
热议问题