python 如何判斷一個數爲整數?(判斷整數,沒有小數)(取餘)判斷整型 isinstance()

方法1 判斷是否爲整數(即使不爲整型)

# -*- coding: utf-8 -*-
"""
@File    : test.py
@Time    : 2020/6/25 11:27
@Author  : Dontla
@Email   : [email protected]
@Software: PyCharm
"""
a = 1.004
b = 0.00
c = 0.543
d = 55
print(a % 1 == 0)  # False
print(b % 1 == 0)  # True
print(c % 1 == 0)  # False
print(d % 1 == 0)  # True

方法2 判斷是否爲整型(int)

# -*- coding: utf-8 -*-
"""
@File    : test.py
@Time    : 2020/6/25 11:27
@Author  : Dontla
@Email   : [email protected]
@Software: PyCharm
"""
a = 1.004
b = 0.00
c = 0.543
d = 55
print(isinstance(a, int))  # False
print(isinstance(b, int))  # False
print(isinstance(c, int))  # False
print(isinstance(d, int))  # True

參考文章1:python判斷一個數是否能被另一個整數整除

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章