Odoo中文网|Odoo实施培训
标题:
Python 自动给数字前面补0
[打印本页]
作者:
jeffrey
时间:
2018-7-31 17:32
标题:
Python 自动给数字前面补0
Python中有一个zfill函数 用来给字符串前面补零
n = "123"
s = n.zfill(5)
assert s == '00123'
zfill也可以给负数补0:
n = '-123'
s = n.zfill(5)
assert s == '-0123'
对于纯数字也可以通过格式化的方式来补0:
n = 123
s = '%05d' % n
assert s == '00123'
欢迎光临 Odoo中文网|Odoo实施培训 (http://www.chinaodoo.net/)
Powered by Discuz! X3.2