简体   繁体   English

如果小数点前没有值,则加零(在Python 2.7中)

[英]If no value before decimal, then add zero (in Python 2.7)

I have the following values: 我有以下值:

.03
.04
1.23

I would like to add 0 to any of the values that do not have a value before the decimal place. 我想在小数点前没有值的任何值上加上0。 So the output would be: 因此输出为:

0.03
0.04
1.23

assuming that what you have are strings, in this case, ever the ever so simple 假设您拥有的是字符串,在这种情况下,那么简单

print (float(a)) 

works example 工程实例

print (float(".03"))

0.03 0.03

if n is your number, this should get you what you want 如果n是您的电话号码,这应该可以为您提供所需的信息

'%03.2f' % n

If you want other options, see this link https://pyformat.info/#number for plenty of information about formatting numbers and strings in python. 如果需要其他选项,请参见此链接https://pyformat.info/#number ,以获取有关在python中格式化数字和字符串的大量信息。

try: 尝试:

z = format(.03, 'f')
print z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM