Python's correct method for Division operations on decimals

Source: Internet
Author: User
This article describes how to use Python to perform Division operations on decimals. The correct method is to convert it to a floating point. Otherwise, the correct result will never be obtained, for more information, see the following formula.

The Code is as follows:


A = 1
B = 2
C = 3

Print c * (a/B)


The running result is always 0. After repeated check and splitting, it is found that in Python, the first integer can only be an integer.
That is, the result of dividing a by B is always 0. You only need to change the number of a or B to a floating point number.

The Code is as follows:


A = 1
B = 2
C = 3

Print c * (a/float (B ))
Print c * (float (a)/B)


In this way, the correct result of Division B can be accurately calculated. Of course, if a is larger than B, and float is not required.
For example:

The Code is as follows:


A = 1
B = 2
C = 3

Print c/a #3
Print c/B #1
Print c/float (B) #1.5

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.