除法的运算
‘/’ 无论是否整除返回的都是 float ,暂且叫它精确除法
例如 : 10/5,的到的结果是 2.0
‘//’无论是否整除返回的都是 int ,而且是去尾整除
例如 :5//2,得到的结果是 2
向上向下取整
要先导入模块 math
向上取整
math.ceil()
返回值为 int
向下取整
math.floor()
返回值为 int
四舍五入
内置函数 round()
返回值为 int
例子:
#encoding:utf-8
import math
#向上取整
print "math.ceil---"
print "math.ceil(2.3) => ", math.ceil(2.3)
print "math.ceil(2.6) => ", math.ceil(2.6)
#向下取整
print "\nmath.floor---"
print "math.floor(2.3) => ", math.floor(2.3)
print "math.floor(2.6) => ", math.floor(2.6)
#四舍五入
print "\nround---"
print "round(2.3) => ", round(2.3)
print "round(2.6) => ", round(2.6)
#这三个的返回结果都是浮点型
print "\n\nNOTE:every result is type of float"
print "math.ceil(2) => ", math.ceil(2)
print "math.floor(2) => ", math.floor(2)
print "round(2) => ", round(2)
输出结果
问题:round函数有争论
链接:https://juejin.im/post/6844903810293301255?utm_source=gold_browser_extension