I need to check if a float number has decimals or not.

This is what I've tried to do this far but it doesn't seem to work. What am i doing wrong, is there a better way?

n =[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]
a = [1.5,2]
b = a[0] 

for i in range(len(n)):
	if b in n: print'yes,',b,'in',n
	else: print 'no,',b,'not in',n
	b = b + 0.1

Result:

no, 1.5 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 1.6 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 1.7 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 1.8 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 1.9 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 2.0 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 2.1 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 2.2 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 2.3 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
no, 2.4 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]

I don't see why this line say no, 2.0 is clearly in the list:
no, 2.0 not in [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]

Recommended Answers

All 4 Replies

You are trying to compare floating point numbers. The computer deals with floating point numbers internally in binary form. This leads to small roundoff errors. Just about all computer languages have that problem

When you are calculating
b = b + 0.1
by the time you get to
b = 2.0
you actually have
b = 2.0000000000000004

You can test it out this way ...

list_b = []
b = 1.5
for i in range(10):
    list_b.append(b)
    b = b + 0.1
    
print list_b

"""my result -->
[1.5, 1.6000000000000001, 1.7000000000000002, 1.8000000000000003, 
1.9000000000000004, 2.0000000000000004, 2.1000000000000005, 
2.2000000000000006, 2.3000000000000007, 2.4000000000000008]
"""

Python has a module decimal that helps you prevent this roundoff problem.

I need to check if a float number has decimals or not.

Convert to a string, split on the ".", and see if the number after the decimal is greater than zero. You can also use divmod which returns the whole and decimal parts of a number
x = 2.5
print divmod(x, 1)
(2.0, 0.5)

You are trying to compare floating point numbers. The computer deals with floating point numbers internally in binary form. This leads to small roundoff errors. Just about all computer languages have that problem

So that's why, thank you for explaining!

Convert to a string, split on the ".", and see if the number after the decimal is greater than zero. You can also use divmod which returns the whole and decimal parts of a number
x = 2.5
print divmod(x, 1)
(2.0, 0.5)

split() worked for me. This is how I solved it:

x = 1.5
for i in range(10):
        a = str(x).split('.')
        b = int(a[1])
        if b == 0:
                print x, 'has no decimals'
        else: print a
        x += 0.1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.