How to check if a numeric value is an integer?

后端 未结 1 1874
南旧
南旧 2021-01-26 10:19

I need to check if a value is an integer in Python. Note that by integer I mean values like 2, 1.0 and -4.0000, whereas 0.4 a

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 10:42

    float instances have an is_integer method, which tells you whether f == int(f). The following snippet will therefore work for both integers and floats, as well as any strings representing either of those numerical types:

    float(value).is_integer()
    

    0 讨论(0)
提交回复
热议问题