Python Digital Series-Digitally formatted output

Source: Internet
Author: User

Formatted output problems for numbers

You need to format the numbers and output them, and control the number of digits, alignment, thousand separators, and other details.

Solution Solutions

When you format the output of a single number, you can use the built-in format() functions, such as:

1>>> x = 1234.567892 3>>>#Both decimal places of accuracy4>>> Format (x,'0.2f') #无空格, decimals reserved 2-bit5 '1234.57'6 7>>>#Right justified in ten chars, one-digit accuracy8>>> Format (x,'>10.1f') #数字输出靠右 with a total length of 10 and decimals reserved 1 bits9 '1234.6'Ten  One>>>#Left justified A>>> Format (x,'<10.1f') #数字输出靠左 with a total length of 10 and decimals reserved 1 bits - '1234.6' -  the>>>#Centered ->>> Format (x,'^10.1f') #数字输出靠中, decimals reserved 2-bit - '1234.6' -  +>>>#inclusion of thousands separator ->>> Format (x,',') #指定逗号位数字的千分位分隔符 + '1,234.56789' A>>> Format (x,'0,.1f') #无占位输出, leave 1 decimal places with commas as separators at '1,234.6' ->>>
########################################################
# Note:
# The number of decimal digits specified at that time is displayed as a rounding effect by default, which is the same as the round effect.
########################################################

If you want to use exponential notation, change F to E or E (depending on the capitalization of the exponential output). Like what:

1 ' e ' ) #指定为科学计数法 2'1.234568e+03'3'0.2E ' ) #指定科学计数法和小数表达式的小数位数 4'1.23E+03'5 >>>

The general form of specifying both width and precision is ‘[<>^]?width[,]?(.digits)?‘ , where width and digits for integers,? Represents an optional section. The same format is used in the method of the string format() . Like what:

1 ' The value is {: 0,.2f} ' . Format (x)  #注意在用于格式化时的数字格式化指定时需要用: Start characterization 2'thevalue is 1,234.57'  3 >>>

Discuss

Formatting that contains thousands characters is not related to localization. If you need to display thousands of characters by region, you need to investigate the locale functions in the module yourself. You can also use string translate() methods to swap thousands of characters. Like what:

1>>> swap_separators = {Ord ('.'):',', Ord (','):'.' }2>>> Format (x,','). Translate (swap_separators)3 '1.234,56789'4>>>

In many Python code, you will see the use of% to format numbers, such as:

1>>>'%0.2f'%x2 '1234.57'3>>>'%10.1f'%x4 '1234.6'5>>>'%-10.1f'%x6 '1234.6'7>>>

This format method is also feasible, but it is almost as far as the more advanced format() . For example, when using the% operator to format numbers, some features (adding thousands of characters) are not supported.

Python Digital Series-Digitally formatted output

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.