Safest way to convert float to integer in python?

后端 未结 10 2553
不思量自难忘°
不思量自难忘° 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 13:56

    math.floor will always return an integer number and thus int(math.floor(some_float)) will never introduce rounding errors.

    The rounding error might already be introduced in math.floor(some_large_float), though, or even when storing a large number in a float in the first place. (Large numbers may lose precision when stored in floats.)

提交回复
热议问题