python2精确除法

python2和python3除法的最大区别:html

python2:python

print 500/1000

python2结果:取整数部分,小数并无保留web

0

Process finished with exit code 0

 

python3:ide

print 500/1000

python3结果:获得真实结果,小数保留url

0.5

Process finished with exit code 0

 

那么,若是python2想保留小数部分,要怎么作呢?spa

只须要增长一个导入包.就能够了.并不须要其它操做code

from __future__ import division #用于/相除的时候,保留真实结果.小数

 

增长导入包后的,python2操做:htm

#coding:utf-8
from __future__ import division

print 500/1000

结果:blog

0.5

Process finished with exit code 0

 

还有另外一种方式.将除数或被除数两个其它至少一个转换成float型:ip

print float(500)/1000

结果:

0.5

Process finished with exit code 0

 

相关文章
相关标签/搜索