Python example of the correct way to divide decimals

Source: Internet
Author: User
Find a formula

The code is as follows:


A=1
b=2
C=3

Print c* (A/b)


The running result is always 0, and after repeated checks are taken apart, it is found that in Python, integers can only be derived from integers.
That is, the result of a except B is always 0, as long as a or b is changed 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 order to accurately calculate the correct result of a except B, of course, if a is larger than B, and does not need the fractional digits, it can be used without float.
Such as:

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.