垫蟒蛇漂浮 [英] Pad python floats

查看:202
本文介绍了垫蟒蛇漂浮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填补一些百分比值,所以在小数位之前总是有3个单位。有了整数,我可以使用'%03d' - 是否有一个相当于浮动?

'%。3f'适用于小数点后的位置,但'%03f'不起作用。 解决方案

'%03.1f'工作(1可以是任何数字或空字符串):

$ p $ >>> %06.2f%3.3
'003.30'

>>> %04.f%3.2
'0003'

请注意,字段宽度包括小数和小数位。


I want to pad some percentage values so that there are always 3 units before the decimal place. With ints I could use '%03d' - is there an equivalent for floats?

'%.3f' works for after the decimal place but '%03f' does nothing.

解决方案

'%03.1f' works (1 could be any number, or empty string):

>>> "%06.2f"%3.3
'003.30'

>>> "%04.f"%3.2
'0003'

Note that the field width includes the decimal and fractional digits.

这篇关于垫蟒蛇漂浮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆