Taking the floor of a float

前端 未结 6 483
礼貌的吻别
礼貌的吻别 2021-02-01 13:21

I have found two ways of taking floors in Python:

3.1415 // 1

and

import math
math.floor(3.1415)

The problem

6条回答
  •  执念已碎
    2021-02-01 13:51

    Beware that taking the floor and casting to an int are not the same thing with negative numbers. If you really want the floor as an integer, you should cast to an int after calling math.floor().

    >>> int(-0.5)
    0
    >>> math.floor(-0.5)
    -1.0
    >>> int(math.floor(-0.5))
    -1
    

提交回复
热议问题