In python if I divide 6/3 I get 2, that's fine. But I divide 6/4 and get 1...

Why aren't I getting a decimal? I would like to know because I want to make something that can detect wether or not a number is whole. (I need help with that too). But I thought I would give it a shot myself and found that it wont give me a decimal when it should.

Thanks

you need to put a decimal before doing the division:

>>> 6.0/4
1.5
>>> 6/4.0
1.5

or you can import future:

>>> from __future__ import division
>>> 6/4
1.5
>>>

Thanks for the help, it worked, but;

Is there a way to detect whole numbers, if they're there.

for example:

I want to do this in python:

a = 2.1
b = 2.0
c = 2

a == False
b == True
c == True

In python if I divide 6/3 I get 2, that's fine. But I divide 6/4 and get 1...

Why aren't I getting a decimal? I would like to know because I want to make something that can detect wether or not a number is whole. (I need help with that too). But I thought I would give it a shot myself and found that it wont give me a decimal when it should.

Thanks

Here is one way to do this:

a = 6
b = 4
# assure a float division
print a/float(b)  # 1.5
# test for integer (whole number)
print type(a) == int  # True

Thanks guys this helps alot, the only thing now though, is that if I have, let's say, 2.0, both of those suggested things return false (unless I'm using them wrong)

I need to find the prescense of a whole number, not if a decimal is there or not. Is there a way to do that?

Thanks guys this helps alot, the only thing now though, is that if I have, let's say, 2.0, both of those suggested things return false (unless I'm using them wrong)

I need to find the prescense of a whole number, not if a decimal is there or not. Is there a way to do that?

Do you want to include decimals like 2.0 , 2.00000 etc as a whole number ??

You could try

if x == int(x):
   print "An integer!"
else:
   print "Not"

or my favorite,

if x % 1 == 0:
   print "Another integer!"
else:
   print "Not."

Jeff

This question has already been answered. Start a new discussion instead.