Python built-in function (--round)

Source: Internet
Author: User



English documents:


round( number[, ndigits]) Return The floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it returns the nearest integer to its input. Delegates tonumber.__round__(ndigits).For the built-in types supportinground(), values is rounded to the closest multiple of ten to the power minus Ndigi TS; If the multiples is equally close, rounding is do toward the even choice (so, for example, both and wasround(0.5)round(-0.5)0, andround(1.5)is2). The return value is a integer if called with one argument, otherwise of the same type as number. Note:


round()the behavior of floats can be surprising:for example,round(2.675, 2)gives instead of the2.67expected2.68. This was 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.






Description



1. The round function is used for rounding the floating-point numbers, reserving several decimals, and controlling them with the incoming ndigits parameter.


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


2. The ndigits parameter is an optional parameter that, when not passed in, is rounded by the default of 0 decimal places, and returns an integer.


>>> round (1.1314926)1


3. The Ndigits parameter is passed in 0 o'clock, although 0 decimal places are rounded up as if the Ndigits parameter is not passed in, but the returned value is floating-point.


>>> round (1.1314926, 0)1.0


4. The ndigits parameter is less than 0 o'clock, rounding the integer part, the ndigits parameter controls the rounding of the number of the integer part of the floating-point number, the fractional part is cleared 0, and the return type is a floating-point number. Returns 0.0 if the integer portion of the incoming floating-point number is less than the absolute value of the Ndigits parameter.


>>> 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. Round rounding is followed near the 0 principle, so-0.5 and 0.5 are rounded 0 bits, and return is 0.


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


6. There is a trap for rounding floating-point numbers, and some rounding results are not as expected, asround(2.675, 2)the results are2.67rather than expected2.68,This is not a bug, but because floating-point numbers are stored with a limited number of digits, there is a certain error between the actual stored value and the displayed value.


>>> round (2.675, 2)2.67


7. You can also perform round operations on integers, and return values are also shaped.


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





Python built-in function (--round)


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.