Warning: session_start(): open(/tmp/sess_30b3964b8f41151e4e79975719d0f770, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Force Division To Return A Floating-Point Number In Python - LearnShareIT

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

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

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:

Leave a Reply

Your email address will not be published. Required fields are marked *