Check if the number is int or float

Here is how I did it:

inNumber = somenumber inNumberint = int(inNumber) if inNumber == inNumberint: print "this number is an int" else: print "this number is a float" 

Something like that. Are there any nicer ways to do this?

+167
python
Dec 27 2018-10-12-27
source share
15 answers

Use isinstance .

 >>> x = 12 >>> isinstance(x, int) True >>> y = 12.0 >>> isinstance(y, float) True 

So:

 >>> if isinstance(x, int): print 'x is a int!' x is a int! 

_EDIT: _

As already noted, in the case of long integers, the above will not work. So you need to do:

 >>> x = 12L >>> import numbers >>> isinstance(x, numbers.Integral) True >>> isinstance(x, int) False 
+241
Dec 27 '10 at 7:16
source share

I like @ninjagecko to answer the most.

This will also work:

for Python 2.x

 isinstance(n, (int, long, float)) 

Python 3.x is not long

 isinstance(n, (int, float)) 

there is also a type complex for complex numbers

+73
Feb 18 '16 at 16:54
source share

Single line:

 isinstance(yourNumber, numbers.Real) 

This avoids some problems:

 >>> isinstance(99**10,int) False 

Demo:

 >>> import numbers >>> someInt = 10 >>> someLongInt = 100000L >>> someFloat = 0.5 >>> isinstance(someInt, numbers.Real) True >>> isinstance(someLongInt, numbers.Real) True >>> isinstance(someFloat, numbers.Real) True 
+47
Aug 22
source share

It’s easier to ask for forgiveness than to ask permission. Just do the operation. If it works, the object has an acceptable, suitable, correct type. If the operation does not work, the object was not of the appropriate type. Knowing the type rarely helps.

Just try the operation and see if it works.

 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 
+13
Dec 27 '10 at 7:23
source share

You can also use type() Example:

 if type(inNumber) == int : print "This number is an int" elif type(inNumber) == float : print "This number is a float" 
+8
May 01 '14 at 10:23
source share

Here's a code snippet that checks if a number is an integer or not, it works for both Python 2 and Python 3.

 import sys if sys.version < '3': integer_types = (int, long,) else: integer_types = (int,) isinstance(yourNumber, integer_types) # returns True if it an integer isinstance(yourNumber, float) # returns True if it a float 

Note that Python 2 has both int and long types, while Python 3 has only int type. Source

If you want to check if your number is a number with a float representing int , do this

 (isinstance(yourNumber, float) and (yourNumber).is_integer()) # True for 3.0 

If you don't need to distinguish between int and float, and you agree with any of them, then ninjagecko's answer is the way to go

 import numbers isinstance(yourNumber, numbers.Real) 
+5
Apr 29 '15 at 18:10
source share

You can use modulo to determine if x is an integer. The isinstance(x, int) method only determines whether x is an integer of type:

 def isInt(x): if x%1 == 0: print "X is an integer" else: print "X is not an integer" 
+5
Jun 04 '17 at 1:29 on
source share

I know this is an old topic, but this is what I use, and I thought it might help.

It works in Python 2.7 and Python 3 <.

 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)) 
+5
May 20 '18 at 16:37
source share

pls check this: import numbers

 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 

Although X is a float, the value is an integer, so if you want to check the integer value, you cannot use isinstance, and you need to compare the values, not the types.

+4
Feb 01 '15 at 14:40
source share

how about this solution?

 if type(x) in (float, int): # do whatever else: # do whatever 
+3
Dec 6 '17 at 10:50
source share

Tried Shell version 3.6.3 in Python

 >>> x = 12 >>> import numbers >>> isinstance(x, numbers.Integral) True >>> isinstance(x,int) True 

I can’t understand what to work for.

+2
Mar 22 '18 at 5:03
source share
 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 
-one
Nov 03 '17 at 18:12
source share

Try this...

 def is_int(x): absolute = abs(x) rounded = round(absolute) return absolute - rounded == 0 
-one
Jan 21 '18 at 12:44
source share

You can simply do if int(inNumber): or if float(inNumber):

-one
Jun 21 '19 at 8:17
source share

variable.isnumeric checks if an integer is:

  if myVariable.isnumeric: print('this varibale is numeric') else: print('not numeric') 
-5
Jun 28 '16 at 8:36
source share



All Articles