python2精确除法奥门金沙手机娱乐网址

作者:奥门金沙手机娱乐网址    发布时间:2019-12-16 08:00    浏览:181 次

[返回]

那么,如果python2想保留小数部分,要怎么做呢?

 

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

python2结果:取整数部分,小数并没有保留

 

print 500/1000

 

python2:

T

#coding:utf-8
from __future__ import division

print 500/1000
print 500/1000

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

M

 

0.5

Process finished with exit code 0

python3:

Text-to-speech function is limited to 200 characters

Detect language Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu   Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu          

增加导入包后的,python2操作:

 

 

 

结果:

 

0.5

Process finished with exit code 0
print float(500)/1000
  Options : History : Feedback : Donate Close

结果:

python2精确除法奥门金沙手机娱乐网址。G

 

只需要增加一个导入包.就可以了.并不需要其它操作

 

0

Process finished with exit code 0

 

python2和python3除法的最大区别:

奥门金沙手机娱乐网址 , 

 

0.5

Process finished with exit code 0

python3结果:python2精确除法奥门金沙手机娱乐网址。得到真实结果,小数保留

搜索