# Best way to see if a number is fractional or not

• A+
Category：Languages

I'm looking to differentiate between a number like `2.0` or `2` and an actual fractional number such as `2.4`. What would be the best way to do this? Currently I'm doing:

``def is_fractional(num):     if not str(num).replace('.','').isdigit(): return     return float(num) != int(num)  >>> is_fractional(2) False >>> is_fractional(2.1) True >>> is_fractional(2.0) False >>> is_fractional('a') >>> ``

That operation is built-in:

``>>> 5.0.is_integer() True >>> 5.00000001.is_integer() False >>> 4.9999999.is_integer() False ``

Documentation is here.

The initial solution only works for `float`. Here's a more complete answer, with tests:
``from decimal import Decimal  def is_integer(x):     if isinstance(x, int):         return True     elif isinstance(x, float):         return x.is_integer()     elif isinstance(x, Decimal):         return x.as_integer_ratio()[1] == 1     return False  good = [     0,      0.0,      3,      -9999999999999999999999,      -2.0000000000000,     Decimal("3.000000"),     Decimal("-9") ] bad = [     -9.99999999999999,     "dogs",     Decimal("-4.00000000000000000000000000000000001"),     Decimal("0.99999999999999999999999999999999999") ]  for x in good:     assert is_integer(x) for x in bad:     assert not is_integer(x) print("All tests passed") ``