Python----decimal point precision control round () __python

Source: Internet
Author: User

The Python version also affects the results, and python2 the x round to the nearest multiple from 0, such as round (0.5) =1, round (-0.5) =-1;

Python3 rounds x to the nearest number of even multiples, such as round (0.5) =0, round (1.5) =2.0, round (2.5) = 2.0, and the following are the output of the python2.7

1. Direct Output

Division: An integer that is divisible by a decimal point.

Print 6/2
print 6/2.0
print 6/2.00
print 6/2.000
print 6.0/2
print 6.00/2
print 6.000/2

Results:

3

3.0

3.0

3.0

3.0

3.0

3.0

Non-division: Real Results

Print 6.1/2 print
6.10/2 print
6.100/2
print 6.11/2

2. Precision Control

High precision---Low precision round ()

Round ()-do not specify, rounding, rounded (if encountered. 5 accidentally discard Odd)

Print round (2.3) Print
round (2.6) Print
round (2.5)
print round (1.5)


Round ()--Specify the number of decimal places, rounded (if encountered. 5 on the contrary, I enter a singular discard)

Print round (2.635, 2) Print
round (2.645, 2) print round
(2.655, 2) print round (
2.665, 2)
print round ( 2.675, 2)


Formatting

Print ("%.2f"% 2.635)
print int (2.635)

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.