python浮点数与整数间的转化

  • 舍弃小数部分  

    >>> math.trunc(12.533222)
    12   

    >>> round(12.2544)
    12

也可以:

int(12.0)=12

 

 

  • 按给定小数位数四舍五入

    >>> round(12.2544, 3)
    12.254

  • 向上取整

    >>> math.ceil(12.4584)

    13

  • 向下取整

    >>> math.floor(12.4584)

    12

posted on 2019-12-27 11:45  一杯明月  阅读(1928)  评论(0编辑  收藏  举报