Python built-in functions (55) -- round, python built-in 55 round

Source: Internet
Author: User

Python built-in functions (55) -- round, python built-in 55 round

English document:

round( Number[, Ndigits])
Return the floating point value NumberRounded NdigitsDigits after the decimal point. If NdigitsIs omitted, it returns the nearest integer to its input. Delegates number.__round__(ndigits).
For the built-in types supporting round(), Values are rounded to the closest multiple of 10 to the power minus Ndigits; If two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5)And round(-0.5)Are 0, And round(1.5)Is 2). The return value is an integer if called with one argument, otherwise of the same type Number.
Note:

The behaviorround()For floats can be surprising: for example,round(2.675, 2)Gives2.67Instead of the expected2.68. This is not a bug: it's a result of the fact that most decimal fractions can't be represented exactly as a float. see Floating Point Arithmetic: Issues and Limitations for more information.

 

Note:

1. The round function is used to round the floating point number to evaluate the value. It retains a few decimal places and is controlled by the passed ndigits parameter.

>>> round(1.1314926,1)1.1>>> round(1.1314926,5)1.13149

2. The ndigits parameter is an optional parameter. If this parameter is not input, an integer is returned.

>>> round(1.1314926)1

3. When the ndigits parameter is set to 0, the returned value is a floating point type, although the value of the zero decimal point is retained as the ndigits parameter is not set.

>>> round(1.1314926,0)1.0

4. when the ndigits parameter is less than 0, the integer part is rounded. The ndigits parameter controls the last few digits of the integer part of the floating point number, and the decimal part is all 0. The return type is a floating point number. If the integer part of the input floating point is smaller than the absolute value of the ndigits parameter, 0.0 is returned.

>>> round(11314.926,-1)11310.0>>> round(11314.926,-3)11000.0>>> round(11314.926,-4)10000.0>>> round(11314.926,-5)0.0

5. When round rounding is followed by the principle of approaching 0, so-0.5 and 0.5 are rounded to 0, and 0 is returned.

>>> round(0.5)0>>> round(-0.5)0

6. There is a trap for rounding floating point numbers. Some rounding results are not as expected, suchround(2.675, 2)The result is2.67Not as expected2.68. This is not a bug, but because the number of digits is limited when the floating point is stored, there is a certain error between the actually stored value and the displayed value.

>>> round(2.675, 2)2.67

7. The round operation can also be performed on integers, And the return value is also an integer.

>>> round(134567)134567>>> round(134567,0)134567>>> round(134567,1)134567>>> round(134567,2)134567>>> round(134567,-2)134600>>> round(134567,-6)0

 

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.