打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python int() round() floor()函数的比较

猛的一看  int() round()  math.floor() 这几个函数函数好像做的是同一件事情,很容易将他们弄混,下面是他们的一些不同之处:

  • int()函数直接截去小数部分
  • floor() 得到最接近原数但是小于原数的部分
  • round()得到最接近原数的整数(返回为浮点类型)

如下面的例子:

import math
for eachnum in (.2,.7,1.2,1.7,-.2,-.7,-1.2,-1.7):
 print"int(%.1f)\t%+.1f"%(eachnum,float(int(eachnum)))
 print"floor(%.1f)\t%+.1f"%(eachnum,math.floor(eachnum))
 print "round(%.1f)\t%+.1f"%(eachnum,round(eachnum))
 print'-'*20

>>>
int(0.2) +0.0
floor(0.2) +0.0
round(0.2) +0.0
--------------------
int(0.7) +0.0
floor(0.7) +0.0
round(0.7) +1.0
--------------------
int(1.2) +1.0
floor(1.2) +1.0
round(1.2) +1.0
--------------------
int(1.7) +1.0
floor(1.7) +1.0
round(1.7) +2.0
--------------------
int(-0.2) +0.0
floor(-0.2) -1.0
round(-0.2) -0.0
--------------------
int(-0.7) +0.0
floor(-0.7) -1.0
round(-0.7) -1.0
--------------------
int(-1.2) -1.0
floor(-1.2) -2.0
round(-1.2) -1.0
--------------------
int(-1.7) -1.0
floor(-1.7) -2.0
round(-1.7) -2.0
--------------------
>>>

 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python 之 向上取整、向下取整以及四舍五入函数
C# 之 Math取整
js 除法 取整
Python常用内置函数
Delphi取整函数
Python floor() 函数 | 菜鸟教程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服