Python Glossary Русский

all Python 3Numbers & mathfunction

float.is_integer

as a fraction

Return True if the float has an integral value (no fractional part), else False.

Syntax

x.is_integer()

This is a statement about the value, not the type: 4.0 is still a float, and is_integer() converts nothing — use int() for that. The usual trap is rounding: a computed result that is mathematically whole often differs in the last bits and returns False, while inf and nan always return False. At the other end, floats with magnitude 2**52 or larger are spaced at least one apart, so every one of them reports True.

Examples

print((4.0).is_integer())  # → True
print((4.5).is_integer())  # → False
print((-2.0).is_integer())  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy