Home > Backend Development > Python Tutorial > How to use %f in python

How to use %f in python

爱喝马黛茶的安东尼
Release: 2019-07-13 17:57:13
Original
12379 people have browsed it

How to use %f in python

How to use %f in python? Let me introduce to you the related usage of %f.

Related recommendations: "Python Video Tutorial"

import math    
#%a.bf,a表示浮点数的打印长度,b表示浮点数小数点后面的精度    
#只是%f时表示原值,默认是小数点后5位数  
print "PI=%f" % math.pi             
# output: PI=3.141593  

#只是%9f时,表示打印长度9位数,小数点也占一位,不够左侧补空格  
print "PI=%9f" % math.pi            
# output: PI=_3.141593  

#只有.没有后面的数字时,表示去掉小数输出整数,03表示不够3位数左侧补0  
print "PI=%03.f" % math.pi          
# output: PI=003  

#%6.3f表示小数点后面精确到3位,总长度6位数,包括小数点,不够左侧补空格  
print "PI=%6.3f" % math.pi          
# output: PI=_3.142  
  
#%-6.3f表示小数点后面精确到3位,总长度6位数,包括小数点,不够右侧补空格  
print "PI=%-6.3f" % math.pi         
# output: PI=3.142_  
  
#还可以用%*.*f来表示精度,两个*的值分别在后面小括号的前两位数值指定  
#如下,不过这种方式06就失去补0的功能,只能补空格  
print "PI=%*.*f" % (06,3,math.pi)   
# output: PI=_3.142
Copy after login

The above is the detailed content of How to use %f in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template