Round a Floating Point Number Down to the Nearest Integer?

前端 未结 12 1440
悲哀的现实
悲哀的现实 2020-12-02 13:45

As the title suggests, I want to take a floating point number and round it down to the nearest integer. However, if it\'s not a whole, I ALWAYS want to round down the variab

12条回答
  •  伪装坚强ぢ
    2020-12-02 14:33

    a lot of people say to use int(x), and this works ok for most cases, but there is a little problem. If OP's result is:

    x = 1.9999999999999999
    

    it will round to

    x = 2
    

    after the 16th 9 it will round. This is not a big deal if you are sure you will never come across such thing. But it's something to keep in mind.

提交回复
热议问题