# How To Force Division To Return A Floating-Point Number In Python

To solve the problem how to force division to return a floating-point number in Python, there are some solutions we have effectively tested. Follow the article to better understand.

## Force division to return a floating-point number in Python?

For example, I have two positive integer values, and I need to divide them and return the result in floating point.

I will give solutions based on the version of Python you are using.

### In Python 3.0 and later

If you’re using Python 3.0 or later, this shouldn’t be a problem because ‘\’ division by two integers always returns a floating point.

Example:

myNumber1 = 10
myNumber2 = 22
division = myNumber1 / myNumber2

print(division)

Output:

0.45454545454545453

The division is always left floating point in Python 3.x

### In Python 2

It’s not as simple as the Python 3 versions to divide two integers in Python 2, and we need to declare the built-in module in Python 2: ‘_furture_’ module.

‘_furture_’ module is a module that programmers use to enable new language functions incompatible with current compilers.

Syntax:

from _furture_ import division

Example:

from __future__ import division

myNumber1 = 10
myNumber2 = 9

print(myNumber1 / myNumber2)

Output:

1.1111111111111112

In the example above, if you want integer division in Python 2, then still use the ‘_furture_’ module in combination with the ‘/’ operator.

Example:

from __future__ import division

myNumber1 = 10
myNumber2 = 9

print(myNumber1 // myNumber2)

Output:

1

In the example above, if you want integer division in Python 2, then still use the ‘_furture_’ module in combination with the ‘//’ operator.

### Use the ‘operator’ module

In Python, the operators provided in the operator module are functions similar to Python’s operators. For example, the function operator.add(x, y) has the same function as x + y. In this way, I will show you the function operator.truediv(a, b).

Syntax:

operator.truediv(a, b)

Parameters:

• a, b can be real or integer.

Method operator.truediv(a, b) will return the result of division a / b as floating point.

Example:

from operator import truediv

myNumber1 = 2.3
myNumber2 = 3.4

result = truediv(myNumber1, myNumber2)
print(result)

Output:

0.676470588235294

In the above value, I declare 2 functions containing two integers, then use the truediv() function to return the result as floating point.

Note: using the operator.truediv() function is powerful, but execution time can be slow since this is a function call.

### Type conversion of the divisor

If you don’t want to use the ‘future’ module, then you can convert the type of divisor from an integer to a real number when performing the calculation will appear floating point.

Example:

myNumber1 = 10
myNumber2 = 9
result = myNumber1 / float(myNumber2)
print(result)

Output:

1.1111111111111112