Home > Backend Development > Python Tutorial > Detailed explanation of how to use Python to obtain floating point values ​​after operations

Detailed explanation of how to use Python to obtain floating point values ​​after operations

伊谢尔伦
Release: 2017-05-31 14:18:12
Original
2237 people have browsed it

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
Copy after login

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
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template