Use of round (x,[n]) in Python

Source: Internet
Author: User
Round (X,[n]) rounding n reserved fractional number

1, when n is not filled, the default is 0, that is, the decimal part is not retained
A, when the fractional part is only 0.5 when the integer part if it is an odd number in 1, if even the fractional part of the
Print (round (1.5)) #2 fractional part is divided into 0.5 integers with an odd number +1
Print (round (2.5)) #2 fractional part is divided into 0.5 integer parts for even number of fractional part
Print (Round (-1.5)) #-2
Print (Round (-2.5)) #-2
Print (round (1.4)) #1
Print (Round (2.4)) #2
Print (Round (-1.4)) #-1
Print (Round (-2.4)) #-2
b, the number of parts is not 0.5 when you press rounding
Print (round (1.54)) #2 decimal 0.54 in 1
Print (round (2.54)) #3
Print (Round (-1.54)) #-2
Print (Round (-2.54)) #-3
Print (Round (1.45)) #1 decimal 0.45
Print (round (2.45)) #2
Print (Round (-1.45)) #-1
Print (Round (-2.45)) #-2

2. When the fractional part is reserved
A, when the fractional part of the position after the number of a 5 o'clock, the number of positions to retain the number of even when 1, odd to go
Print (Round (2.635, 2)) #2.63 the number of reserved digits after a 5, the reserved position is 3 is odd, then does not enter 1
Print (Round (2.645, 2)) #2.65 the number of reserved digits is 5, the reserved position is 4 is even, then the 1
Print (Round (2.655, 2)) #2.65
Print (Round (2.665, 2)) #2.67
Print (Round (2.675, 2)) #2.67
b, when the number of positions after the one is not 5 o'clock, press rounding
Print (Round (2.636, 2)) #2.64
Print (Round (2.646, 2)) #2.65
Print (Round (2.656, 2)) #2.66
Print (Round (2.666, 2)) #2.67
Print (Round (2.676, 2)) #2.68

  • 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.