inNumber = somenumber
try:
inNumberint = int(inNumber)
print "this number is an int"
except ValueError:
pass
try:
inNumberfloat = float(inNumber)
print "this number is a float"
except ValueError:
pass
import math
a = 1.1 - 0.1
print a
print isinstance(a, numbers.Integral)
print math.floor( a )
if (math.floor( a ) == a):
print "It is an integer number"
else:
print False
def is_int(x):
absolute = abs(x)
rounded = round(absolute)
if absolute - rounded == 0:
print str(x) + " is an integer"
else:
print str(x) +" is not an integer"
is_int(7.0) # will print 7.0 is an integer
def is_float(num):
"""
Checks whether a number is float or integer
Args:
num(float or int): The number to check
Returns:
True if the number is float
"""
return not (float(num)).is_integer()
class TestIsFloat(unittest.TestCase):
def test_float(self):
self.assertTrue(is_float(2.2))
def test_int(self):
self.assertFalse(is_float(2))
inNumber = [32, 12.5, 'e', 82, 52, 92, '1224.5', '12,53',
10000.000, '10,000459',
'This is a sentance, with comma number 1 and dot.', '121.124']
try:
def find_float(num):
num = num.split('.')
if num[-1] is not None and num[-1].isdigit():
return True
else:
return False
for i in inNumber:
i = str(i).replace(',', '.')
if '.' in i and find_float(i):
print('This is float', i)
elif i.isnumeric():
print('This is an integer', i)
else:
print('This is not a number ?', i)
except Exception as err:
print(err)