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

Source: Internet
Author: User
In python, round (x, [n]) uses round (x, [n]) to rounding n to retain the number of fractional parts

1. when n is not entered, the default value is 0, that is, the fractional part is not retained.
A. When the decimal part is only 0.5, if the integer part is an odd number, 1 is entered. if the decimal part is an even number, the decimal part is removed.
Print (round (1.5) #2 if the decimal part is 0.5 and the integer part is odd, then + 1
Print (round (2.5) #2. if the decimal part is 0.5 and the integer part is an even number, the decimal part is removed.
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. If the decimal part is not 0.5, rounding
Print (round (1.54) #2 decimal places 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 number 0.45
Print (round (2.45) #2
Print (round (-1.45) #-1
Print (round (-2.45) #-2

2. when the fractional part is retained
A. When the last digit of the decimal part is 5, when the number of reserved locations is an even number, 1 and an odd number are removed.
Print (round (2.635, 2) # The last digit of the 2.63 retention number is 5, and the reserved position is 3, which is an odd number.
Print (round (2.645, 2) # if the last digit of 2.65 is 5 and the position of 4 is an even number, enter 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 last digit is not 5, 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.