A method of dividing two integers in python to get the value of a floating-point number

Source: Internet
Author: User

/*********************************************************************
* Author:samson
* date:09/19/2014
* Test Platform:
* Linux Ubuntu 3.2.0-58-generic-pae
* GNU Bash, version 4.2.39
* *******************************************************************/

When you divide two integers in Python, by default it is only possible to get the value of an integer, and when you need to evaluate the results in a precise way, you want to get the floating-point value after the operation, then how to handle it?

1, modified dividend value for the form of a decimal point can be a floating-point value, this method in the case of dividend prior knowledge to be effective, and this situation means that the value of the divisor is written dead, fixed, in most cases is not feasible;

2, before the division operation to import a real division module, you can divide two integers when the result of floating point,

From __future__ Import Division

The following is the result of the test:

[Email protected]:~$ 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 Division
>>> 244158112/1024
238435.65625
>>> 244158112/1024/1024
232.84732055664062


A method of dividing two integers in python to get the value of a floating-point number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.