Python 检查数字是 int 还是 float
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4541155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a number is int or float
提问by Steinthor.palsson
Here's 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 looking ways to do this?
类似的东西。
有没有更好看的方法来做到这一点?
回答by user225312
Use isinstance.
使用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 pointed out, in case of long integers, the above won't work. So you need to do:
正如所指出的,在长整数的情况下,上述方法将不起作用。所以你需要这样做:
>>> x = 12L
>>> import numbers
>>> isinstance(x, numbers.Integral)
True
>>> isinstance(x, int)
False
回答by S.Lott
It's easier to ask forgiveness than ask permission. Simply perform the operation. If it works, the object was of an acceptable, suitable, proper type. If the operation doesn't work, the object was not of a suitable type. Knowing the type rarely helps.
请求原谅比请求许可更容易。只需执行操作。如果它有效,则该对象是可接受的、合适的、正确的类型。如果操作不起作用,则对象的类型不合适。了解类型很少有帮助。
Simply attempt 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
回答by user274595
What you can do too is usingtype()Example:
您也可以使用type()示例:
if type(inNumber) == int : print "This number is an int"
elif type(inNumber) == float : print "This number is a float"
回答by ninjagecko
One-liner:
单线:
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
回答by M.Hefny
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 float but the value is integer, so if you want to check the value is integer you cannot use isinstance and you need to compare values not types.
虽然 X 是浮点数但值是整数,所以如果你想检查值是整数,你不能使用 isinstance 并且你需要比较值而不是类型。
回答by Agostino
Here's a piece of code that checks whether a number is an integer or not, it works for both Python 2 and Python 3.
这是一段检查数字是否为整数的代码,它适用于 Python 2 和 Python 3。
import sys
if sys.version < '3':
integer_types = (int, long,)
else:
integer_types = (int,)
isinstance(yourNumber, integer_types) # returns True if it's an integer
isinstance(yourNumber, float) # returns True if it's a float
Notice that Python 2 has both types intand long, while Python 3 has only type int. Source.
注意,Python 2中具有两种类型的int和long,而Python 3只键入int。来源。
If you want to check whether your number is a floatthat represents an int, do this
如果您想检查您的号码是否是float代表a 的 a int,请执行此操作
(isinstance(yourNumber, float) and (yourNumber).is_integer()) # True for 3.0
If you don't need to distinguish between int and float, and are ok with either, then ninjagecko's answer is the way to go
如果您不需要区分 int 和 float,并且两者都可以,那么 ninjagecko 的答案就是要走的路
import numbers
isinstance(yourNumber, numbers.Real)
回答by Dan H
I like @ninjagecko's answer the most.
我最喜欢@ninjagecko 的回答。
This would also work:
这也可以:
for Python 2.x
用于 Python 2.x
isinstance(n, (int, long, float))
Python 3.x doesn't have long
Python 3.x 不长
isinstance(n, (int, float))
there is also type complexfor complex numbers
这里还键入复杂的复数
回答by person
variable.isnumericchecks if a value is an integer:
variable.isnumeric检查值是否为整数:
if myVariable.isnumeric:
print('this varibale is numeric')
else:
print('not numeric')
回答by William Gerecke
You can use modulo to determine if x is an integer numerically. The isinstance(x, int)method only determines if x is an integer by type:
您可以使用模数来确定 x 是否为数字整数。该isinstance(x, int)方法仅通过类型确定 x 是否为整数:
def isInt(x):
if x%1 == 0:
print "X is an integer"
else:
print "X is not an integer"
回答by Gideon Lytes
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

