Python built-in functions (1)

Source: Internet
Author: User

Do a few questions and review Python's built-in functions.

Pythonround () function function Description

The round () function returns the rounding value of the floating-point number X

Grammar

Round (x[, N])

Usage Example : floating-point number x=3.1415926, using the round () function to show x two digits after the decimal point
Print (Round (x, 2))

Run Results
3.14

write a little bit more

When using the round () function, sometimes the result is different from what you think, such as:

>>>round (2.25, 1)2.2>>>round (2.35, 1)2.4>>>round (2.2500001, 1) 2.3

Originally, the method used here is not "rounded up", but the banker rounds.

Banker rounding: Four six into five consideration, five after non-zero into one, five after the zero look at parity, five ago for I should shed, five ago for odd to go into a

Combined with the above sentence, and then look at the results of the operation, it is easy to understand.

In addition, both the "rounding" or the banker rounding, which was learned in primary school, is for decimal.

When a binary computer uses this method, it is inevitable that errors will occur.

Specifically, you can refer to the wiki of these two terms, written in very detailed!

Rounding:https://en.wikipedia.org/wiki/rounding

IEEE Floating Point:https://en.wikipedia.org/wiki/ieee_floating_point

Python format control character%m.nf function Description

Used to control specific character widths, data accuracy

Parameter interpretation

M for character width (including decimal point)

If the character width is greater than the M value, M can ignore

If the character width is less than the M value, the right alignment when M is positive, and the left alignment when M is negative

n represents the number of digits after the decimal point

Examples Show

>>>#m can be ignored if the character length is greater than M>>>Print('%5.3f'%123.456789)123.457>>>Print('%-5.3f'%123.456789)123.457>>>#right-justified if the character length is less than m and the M value is positive>>>Print('%20.3f'%123.456789)             123.457>>>#left justified if the character length is less than M and the '-' number is in front of M>>>Print('%-20.3f'%123.456789)123.457


Google took several times to find what it wanted when it looked at the information about the control.
The efficiency is too low, need to learn how to use the search engine efficiently.

The next blog will tidy up the content of this aspect.

Life is too short, flowers open to fold straight must be folded, MO no flower empty folding branches.

Python built-in functions (1)

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.