Python float.is_integer() Method

The float.is_integer() method in Python is a built-in method for float objects. It tells you if a float value can be represented as an integer without losing precision.

If a float has no fractional part after the decimal point (like 20.0), it’s considered an integer from a representational perspective, and float.is_integer() would return True. However, if the float has a decimal component (like 18.5), then float.is_integer() would return False.

Syntax

float_value.is_integer()

Return Value

The method returns a Boolean value:

  • True: if a float is an integer without a fractional part (e.g., 10.0, 256.0)
  • False: if a float value has a fractional part. (e.g., 3.14, 15.75)

Examples

Let’s see how float.is_integer() method works with some examples:

print((1.0).is_integer())     # Output: True (as 1.0 can be represented as an integer)
print((3.14).is_integer())    # Output: False (because it has a fractional part)
print((-1.0).is_integer())    # Output: True (negative but still an integer)

float.is_integer() considers the exact value stored in the float, including any precision issues inherent in floating-point arithmetic. This means even slight discrepancies due to floating-point representation can affect the result. For example:

print((3.0000000000000004).is_integer())  # Output: False (precision issues may cause unexpected results)

Remember that float.is_integer() is only available for the float data type. If you try to use it directly on an integer, you will encounter an AttributeError.

print((5).is_integer())
# AttributeError: 'int' object has no attribute 'is_integer'