Question

So. In my program I have a part where I check whether the result of a division sum is an integer or not. For example, 6 / 3 = 2 (True) or 7 / 3 = 1.66 (False). The problem is that when I do a division like 6 / 3, the result that should be an integer is classed as a float because it comes out as 2.0 instead of 2. Is there any way so that decimal/float answers are classed as floats with a decimal point, and integer answers are classed as an integer? (The number without the .0 at the end)

I have this now:

6 / 3 = 2.0 (float)    
7 / 3 = 1.66 (float)

I want this:

6 / 3 = 2 (integer)    
7 / 3 = 1.66 (float)
Was it helpful?

Solution

Just use float.is_integer().

For example, as expressed by OP:

>>> num1 = 6 / 3  # 2.0
>>> num1.is_integer()
True
>>> num2 = 7 / 3  # 2.33
>>> num2.is_integer()
False

No need for anything complex here- and implementing this into your function should be easy.

OTHER TIPS

Does 7 / 3 * 3 = 7?

return ((n1 // n2 * n2) == n1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top