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

 

相關文章
相關標籤/搜索