Learning Note 1: syntax of the format item {index [, alignment] [: formatstring]}

Source: Internet
Author: User

The syntax of the format item is {index [, alignment] [: formatstring]}, which specifies the optional length and alignment of the mandatory index and formatted text, and an optional string of the format specifiers. The format specifiers are used to control how to set the format of the value of the corresponding object.

Alignment: indicates the placeholder status of the parameter. For example, {0, 8} indicates that the parameter is on the right. If the placeholder value of the parameter is smaller than 8, an extra space (8-number of parameters) is added before the parameter. {0, -8} indicates that the parameter is left. If the placeholder value is less than 8, an extra space (8-number of parameters) is displayed after the parameter.

Formatstring:
C or C
Currency
Convert a number to a string that represents the monetary amount. Conversion is controlled by the currency format information of the numberformatinfo object used to format numbers. The precision specifier indicates the number of decimal places required. If the precision specifier is omitted, the default currency precision given by numberformatinfo is used.
 
D Or d
Decimal number
Only integer types support this format. Convert a number to a string of a decimal number (0-9). If the number is negative, a negative sign is added. The precision specifier indicates the minimum number of digits required in the result string. If necessary, fill the left side of the number with zero to generate the number specified by the precision specifier.
 
E or E
Scientific notation (INDEX)
Convert the number to "-D. DDD... E + DDD "or"-D. DDD... E + DDD string, where each "D" represents a number (0-9 ). If this number is negative, the string starts with a minus sign. There is always a number before the decimal point. The precision specifier indicates the number of digits required after the decimal point. If the precision specifier is omitted, the default value is used, that is, six digits after the decimal point. The case description indicates whether to add the prefix "E" or "e" before the index ". An index is always composed of a positive or negative number and at least three digits. If needed, fill the index with zero to meet the requirements of at least three digits.
 
F or F
Fixed Point
Convert numbers to "-DDD. DDD ..." String, where each "D" represents a number (0-9 ). If this number is negative, the string starts with a minus sign. The precision specifier indicates the number of decimal places required. If the precision specifier is ignored, the default numeric precision given by numberformatinfo is used.
 
G or G
General
Based on the number type and whether there is a precision specifier, the number is converted to the most compact form of a fixed point or scientific notation. If the precision specifier is omitted or zero, the default precision is determined by the number type, as shown in the following table.

Byte or sbyte: 3

Int16 or uint16: 5

Int32 or uint32: 10

Int64 or uint64: 19

Single: 7

Double: 15

Decimal: 29

If the index is greater than-5 and less than the precision specifier when the number is expressed in scientific notation, the fixed point notation is used; otherwise, the scientific notation is used. If a decimal point is required and Zero tail is ignored, the result contains the decimal point. If the precision specifier exists and the number of valid digits in the result exceeds the specified precision, the redundant trailing digits are deleted by rounding. When scientific notation is used, if the format specifier is "g", the result index carries the prefix "e"; if the format specifier is "g ", the index of the result is prefixed with "E ".

The preceding rule has one exception: if the number is decimal and the precision specifier is omitted. In this case, the fixed point notation is always used and the tail zero is retained.

N or N
Number
Convert numbers to "-D, DDD, DDD ..." String, where each "D" represents a number (0-9 ). If this number is negative, the string starts with a minus sign. A thousands separator is inserted between every three digits on the left of the decimal point. The precision specifier indicates the number of decimal places required. If the precision specifier is ignored, the default numeric precision given by numberformatinfo is used.

P or P
Percentage
The number is converted to a percent string defined by the numberformatinfo. percentnegativepattern attribute or the numberformatinfo. percentpositivepattern attribute. If the number is negative, the generated string is defined by percentnegativepattern and starts with a negative number. The converted number is multiplied by 100 as a percentage. The precision specifier indicates the number of decimal places required. If the precision specifier is omitted, the default numeric precision given by numberformatinfo is used.

R or R
Round-trip Process
The description of the round-trip process ensures that the value converted to a string is analyzed as the same value again. When formatting a value using this specifier, first test it using the regular format: Double uses 15-bit precision, and single uses 7-bit precision. If the value is successfully analyzed back to the same value, it is formatted using the regular format specifier. However, if the value is not successfully analyzed as the same value, it is formatted as follows: Double uses 17-bit precision, and single uses 9-bit precision. Although the precision specifier can be appended to the format specifier of the round-trip process, it will be ignored. When this specifier is used, the round-trip process takes precedence over precision. This format is only supported by the floating point type.

X or X
Hexadecimal number
A string that converts a number to a hexadecimal number. The case description indicates whether to use uppercase or lowercase letters for hexadecimal numbers greater than 9. For example, "X" is used to generate "abcdef" and "X" is used to generate "abcdef ". The precision specifier indicates the minimum number of digits required in the result string. If necessary, fill the left side of the number with zero to generate the number specified by the precision specifier. Only integer types support this format.
 

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.