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
Summary
If you have any questions about how to force division to return a floating-point number in Python, leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- Get the length of an Integer in Python
- Round a float DOWN to the nearest Integer in Python
- Check if a string contains a number in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java