Python two integers and floating-point methods to get the value

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 it is necessary to make a precise evaluation of the results obtained, we want to get the floating-point value after the operation, so how to handle it?

1, change dividend value for the form with a decimal point can be obtained floating point value, such method in the case of dividend beforehand know the ability to use effective. This means that the value of the divisor is written dead, fixed, and in most cases is not feasible;

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

From __future__ Import Division

Here are the results 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


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Python two integers and floating-point methods to get values

Related Article

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.