0

I want check a type of a number in python, for example

x=4
y=2
type(x/y) == type(int)
--> False

it has to be True but what python makes is, it takes 4/2 as 2.0.

2.0 is a floating number, how can I make 2.0 to 2 but at the same time I don't want to make 2.5 to 2 for example :

x=5
y=2
type(x/y) == type(int)
--> False

This is what I want. In conclusion I need something that can understand if a number is int or a floating number after a division operation. Can you help me please

Sarper Bilazer
  • 289
  • 2
  • 6

2 Answers2

1

The output of / is going to be a float. You can define your own function that wraps /

import math
def my_div(a, b):
    x = a/b
    if math.floor(x) == x:
        return int(x)
    return x
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

In python 3, x/y will always be of type float, so forget about type

If you only have int numbers as input you could just use modulo:

x%y == 0

yields True if the result is integer, False otherwise

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219