When dividing two integers in python, by default only the value of the integer can be obtained. However, when the result of division needs to be accurately evaluated, you want to perform the operation After that, the floating point value is obtained, so how to process it?
1. Modify the value of the dividend to a form with a decimal point to obtain a floating point value. This method can only be used effectively when the dividend is known in advance, and this situation means Since the value of the dividend is hard-coded and fixed, it is not feasible in most cases;
2. Import a real division module before performing the division operation, so that two integers can be The floating point result is obtained when dividing;
from __future__ import pision
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 ####注意以上的运算结果为整数 ####导入实除法模块后的结果为浮点数 >>> from __future__ import pision >>> 244158112/1024 238435.65625 >>> 244158112/1024/1024 232.84732055664062
[Related recommendations]
1. Python and, or And a summary of and-or syntax
2. Analysis of the usage of and and or in Python
3. Detailed introduction to the actual usage of and and or in Python
The above is the detailed content of Detailed explanation of how to use Python to obtain floating point values after operations. For more information, please follow other related articles on the PHP Chinese website!