python 向上、向下、四舍五入取整方法 round圆整

来源:互联网 发布:minila air mac 编辑:程序博客网 时间:2024/05/16 05:30
import math


#向上取整
>>> math.ceil(2.3)
3
>>> math.ceil(2.6)
3

#向下取整
>>> math.floor(2.3)
2
>>> math.floor(2.6)
2

#四舍五入

>>> round(6.6)
7

>>> round(6.4)
6

>>> round(6.55)
7

=============================================

只要不是  .5 的形式,也就是小数位不为5,round基本用法就是四舍五入

除此之外,round用于圆整:


>>> round(6.5)
6       ---------------------》6是偶数

>>> round(7.5)
8       ---------------------》8是偶数


阅读全文
0 0