Determining whether an value is a whole number in Python
I would like to determine if a numeric value in Python is a whole number. For example, given:
y = x / 3
I want to distinguish between values of x
which are e开发者_如何学运维venly divisible by 3 those which are not.
Integers have no decimals. If you meant "check if a number got decimals in Python", you can do:
not float(your_number).is_integer()
if x % 3 == 0:
print 'x is divisible by 3'
Edit: As Ollie pointed out in the comment below this post, is_integer is part of the standard library and should therefore not be reimplemented as I did below.
This function uses the fact that every other whole number will have at least one number divisible by two with no remainder. Any non-zero fractional representation in either n
or n+1
will cause both n%2
and (n+1)%2
to have a remainder. This has the benefit that whole numbers represented as float values will return True.
The function works correctly for positive
and negative numbers and zero as far as I can determine. As mentioned in the function, it fails for values very close to an integer.
def isInteger(n):
"""Return True if argument is a whole number, False if argument has a fractional part.
Note that for values very close to an integer, this test breaks. During
superficial testing the closest value to zero that evaluated correctly
was 9.88131291682e-324. When dividing this number by 10, Python 2.7.1 evaluated
the result to zero"""
if n%2 == 0 or (n+1)%2 == 0:
return True
return False
x % 3 == 0
will be True
if x / 3
is an integer.
Here's another method:
x = 1/3 # insert your number here
print(x - int(x) == 0) # True if x is a whole number, False if it has decimals.
This works because int(x) essentially takes the floor of the number (ex. 3.6453 -> 3). If there's something left over once you subtract the floor, it can't have been a whole number.
assuming you mean if a string containing digits also has a decimal point:
Python 2.6.6 (r266:84292, Apr 20 2011, 11:58:30)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> number='123.4'
>>> '.' in number
True
>>> number='123'
>>> '.' in number
False
>>>
To test if it's integral you could mod 1:
>>> 1.0/3 % 1
0.33333333333333331
>>> 1/3 % 1
0
In Python 2, dividing an int by an int returns an int (unless python was invoked with the -Qnew
option, or a from __future__ import division
is at the beginning of the source; in that case /
returns a float); a //
specifies integer division.
In Python 3, dividing an int by an int returns a float if you use "/", or an int if you use "//".
If you want to know whether an int will divide into another int exactly, use "%" to look for a remainder.
convert 1.0 => 1 & convert 1.x => 1.x
This code if float numbers has decimal part like 1.5 will return 1.5 & if it is 35.00 it return 35:
a = ReadFrom()
if float(a).is_integer(): # it is an integer number like 23.00 so return 23
return int(a)
else: # for numbers with decimal part like : 1.5 return 1.5
return float(a)
It is best to make your determination before doing the division, assuming that your x variable is an integer.
Trying to do equality tests or comparisons on floating point numbers is dangerous: http://www.lahey.com/float.htm
The answer already provided using modulus before doing the division to see if one integer is divsible by the other integer is safe. After you do a division and are dealing with possibly floating point values, then numbers are no longer exactly integers or not.
精彩评论