Python method of dividing two integers to get a floating point value

  • 2020-04-02 14:40:48
  • OfStack

When dividing two integers in python, by default you can only get the value of the integer, but when you need to accurately evaluate the result of the division, you want to get the floating point value after the operation, so how to deal with it?

1. The floating-point value can be obtained by modifying the value of the dividend to the form of a decimal point. This method can only be effective if the dividend is known in advance.

2. Before division operation, import a module of real division to get floating point results when two integers are divided;


from __future__ import division

The following are the test results:

ufo@ufo:~$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:56)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 244158112/1024
238435
#### Note that the result of the above operation is an integer
#### The result after importing the real division module is a floating point number
>>> from __future__ import division
>>> 244158112/1024
238435.65625
>>> 244158112/1024/1024
232.84732055664062


Related articles: