首页 > 编程知识 正文

python3小数向上取整,python怎么四舍五入取整

时间:2023-05-03 21:19:09 阅读:198615 作者:3454

除法的运算

‘/’ 无论是否整除返回的都是 float ,暂且叫它精确除法
例如 : 10/5,的到的结果是 2.0
‘//’无论是否整除返回的都是 int ,而且是去尾整除
例如 :5//2,得到的结果是 2

向上向下取整

要先导入模块 math

向上取整
math.ceil()
返回值为 int
向下取整
math.floor()
返回值为 int

四舍五入

内置函数 round()
返回值为 int

例子:

#encoding:utf-8import 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 "nnNOTE: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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。