Safest way to convert float to integer in python?

后端 未结 10 2592
不思量自难忘°
不思量自难忘° 2020-11-27 13:44

Python\'s math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer be

10条回答
  •  日久生厌
    2020-11-27 14:14

    You could use the round function. If you use no second parameter (# of significant digits) then I think you will get the behavior you want.

    IDLE output.

    >>> round(2.99999999999)
    3
    >>> round(2.6)
    3
    >>> round(2.5)
    3
    >>> round(2.4)
    2
    

提交回复
热议问题