简体   繁体   中英

Any way so integers are classed as integers with no .0 and floats classed as floats?

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)

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.

7/3 * 3 = 7吗?

return ((n1 // n2 * n2) == n1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM