. Net Common FormatString format characters

Source: Internet
Author: User
Tags iso 8601 rounds

This is often used to format strings of dates and numbers, so we sorted out some of the help items here.

The following table describes the standard format specifiers used to format DateTime objects.
Format description
D. The short date mode displays the mode defined by the DateTimeFormatInfo. ShortDatePattern attribute associated with the current thread or the mode defined by the provider in the specified format.
D. The long date mode displays the mode defined by the DateTimeFormatInfo. LongDatePattern attribute associated with the current thread or the mode defined by the provider in the specified format.
T short-time mode displays the mode defined by the DateTimeFormatInfo. effectimepattern attribute associated with the current thread or the mode defined by the provider in the specified format.
T long time mode displays the mode defined by the DateTimeFormatInfo. LongTimePattern attribute associated with the current thread or the mode defined by the provider in the specified format.
F. The combination of long date and short time modes is displayed in full date/time mode (short time), separated by spaces.
F full date/time mode (long time) displays the mode defined by the DateTimeFormatInfo. FullDateTimePattern attribute associated with the current thread or the mode defined by the provider in the specified format.
G conventional date/time mode (Short Time) shows the combination of short date and short time mode, separated by spaces.
G regular date/time mode (long time) shows the combination of short date and long time mode, separated by spaces.
The M or m month/day mode displays the mode defined by the DateTimeFormatInfo. MonthDayPattern attribute associated with the current thread or the mode defined by the provider in the specified format.
The R or r RFC1123 mode displays the mode defined by the DateTimeFormatInfo. RFC1123Pattern attribute associated with the current thread or the mode defined by the provider in the specified format. This is the defined standard and the attribute is read-only. Therefore, it is always the same regardless of the culture or the provided format provider. The attribute references the CultureInfo. InvariantCulture attribute and follows the custom mode "ddd, dd MMM yyyy HH: mm: ss GMT ". Note that "M" in "GMT" requires an escape character, so it is not interpreted. Formatting does not change the DateTime value, so you must adjust the value to GMT before formatting.
S: sorted date/time mode. ISO 8601 is used to display the mode defined by the DateTimeFormatInfo. SortableDateTimePattern attribute associated with the current thread or the mode defined by the provider in the specified format. The attribute references the CultureInfo. InvariantCulture attribute. The format complies with the custom mode "yyyy-MM-ddTHH: mm: ss ".
U common sortable date/time mode displays the mode defined by the DateTimeFormatInfo. UniversalSortableDateTimePattern attribute associated with the current thread or the mode defined by the provider in the specified format. Because it is a defined standard and its attributes are read-only, the pattern is always the same no matter what the regional or format provider is. The format follows the custom mode "yyyy-MM-dd HH: mm: ssZ ". The Time Zone is not converted when the date and time are formatted. Therefore, convert the local date and time to the common time before the format specifier is used.
The universal sorting date/time mode of U displays the mode defined by the DateTimeFormatInfo. FullDateTimePattern attribute associated with the current thread or the mode defined by the provider in the specified format. Note that the displayed time is the general time, not the local time.
Y or y year-on-year mode displays the mode defined by the DateTimeFormatInfo. YearMonthPattern attribute associated with the current thread or the mode defined by the provider in the specified format.
Unknown specifiers for any other single character

The following table describes the strings in the standard numeric format. Note that the output strings produced by these format specifiers are affected by the settings in the "region options" control panel. Computers with different settings will generate different output strings.

Format description
The C or c currency number is converted into 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 only integer type supports 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 (exponent) 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 numbers are converted to strings in the form of "-ddd...", 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 generally converts numbers into the most compact form of fixed points or scientific notation based on the number type and whether there is a precision specifier. 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.
 
Convert N or n numbers to strings in the "-d, ddd, ddd..." format, 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.
The P or p percentage 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.
The description of the round-trip process of R or r ensures that the value converted to a string is analyzed as the same value again. When formatting a value using this specifier, first use the regular format to test: 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 this 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.
A string that converts X or x hexadecimal numbers to hexadecimal numbers. 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.

If the standard numeric format specifier does not provide the required formatting type, you can use a custom format string to further enhance the string output. A standard string contains a letter, followed by a sequence of numbers (forming a value ranging from 0 to 99), and all other format strings are custom format strings.
The following table shows the strings and characters that can be used to create custom numbers. Note that the setting of the "region options" control panel of the NumberFormatInfo object associated with the current thread affects some output strings of these characters. Computers in different regions will generate different output strings.
Description of Characters in the format
0 0 placeholder if the formatted value contains a number at the position of "0" in the format string, the number is copied to the output string. The leftmost "0" position before the decimal point and the rightmost "0" position after the decimal point determine the total number range in the output string. The "00" specifier rounds the value to the number nearest to the decimal point, and the zero digit is rounded off. For example, if "00" is used to format 34.5, the value 35 is obtained.
# Digit placeholder if the formatted value contains a number at the position of "#" in the format string, the number is copied to the output string. Otherwise, no value is stored in the output string. Note that if "0" is not a valid number, this specifier never displays "0", even if "0" is a unique number in the string. If "0" is a valid number in the displayed number, "0" is displayed. A string in the format of "#" rounds the value to the number nearest to the decimal point, with zero rounded off. For example, format 34.5 with "#" to get the value 35.
. The first "." character in the decimal point format string determines the location of the decimal point Separator in the formatted value. Any other "." characters are ignored. The actual character used as the decimal separator is the NumberDeci of the NumberFormatInfo

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.