Defined
The syntax of a format specifier consists of 3 fields: index number , alignment specifier , and format field . both String.Format and WriteLine adhere to the same formatting rules .
Justification specifier
The alignment specifier represents the minimum width of characters in a field. The justification specifier has the following characteristics.
1, the alignment specifier is optional and is separated by commas and index numbers.
2, it consists of a positive integer or a negative integer.
3, integer indicates the minimum number of characters used in the field.
4. The symbol indicates right or left alignment. Positive numbers indicate right alignment, and negative numbers indicate left alignment.
5, if the number of characters to be represented is less than the number of characters specified in the specifier, then the remaining characters are filled with spaces;
6. If the number of characters to be represented is more than the specified number of characters, the alignment specifier is ignored and the desired character is used for representation.
Format field
The format field specifies the form in which the number should be represented. For example, it should be expressed as a currency, a decimal number, or a fixed-point symbol. The format field has 3 parts.
1, after the colon must follow the format specifier, the middle can not have spaces.
2. The format specifier is an alphabetic character and is one of 9 built-in character formats. The characters can be in uppercase or lowercase form. Casing is important for some specifiers, but not important for other specifiers.
3. The precision specifier is optional and consists of two or more digits. Its actual meaning depends on the format specifier.
Example:
Standard format string
Character |
Description |
Example |
Output |
C |
Currency |
string. Format("{0:c3}", 2) |
¥2.000 (English operating system results: $0.20) |
D |
Decimal |
String. Format ("{0:d3}", 2) |
002 |
F |
Sentinel |
String. format{"0:f4", 123.123} |
123.1230 |
E |
Scientific counting method |
String. format{"0:e4", 0.123456} |
1.2346E+001 |
G |
Conventional |
String. Format ("{0:g}", 2) |
2 |
X |
Hexadecimal |
String. Format ("{0:x000}", 12) |
C |
N |
Comma or space separates numbers |
String. Format ("{0:n}", 250000) |
250,000.00 (default precision 2 bit) |
P |
Percentage |
String. format{"0:p", 0.123456} |
12.34% (default accuracy 2 bit) |
R |
Roundtrip process |
String. format{"0:r", 0.123456} |
0.123456 |
Date formatting
String. Format ("{0:d}", System.DateTime.Now) result is: 2016-9-18 ( Note: month is not 09)
String. Format ("{0:d}", System.DateTime.Now) results are: September 18, 2016
String. Format ("{0:f}", System.DateTime.Now) results are: September 18, 2016 18:14
String. Format ("{0:f}", System.DateTime.Now) results are: September 18, 2016 18:14:26
String. Format ("{0:g}", System.DateTime.Now) results are: 2016-9-18 18:14
String. Format ("{0:g}", System.DateTime.Now) results are: 2016-9-18 18:14:26
String. Format ("{0:m}", System.DateTime.Now) results are: September 18
String. The result of Format ("{0:t}", System.DateTime.Now) is: 18:14
String. The result of Format ("{0:t}", System.DateTime.Now) is: 18:14:26
Similar
string T1 = System.DateTime.Now.ToShortDateString (); T1=2016/9/18
String t2 = System.DateTime.Now.ToShortTimeString (); T2=18:14
string t3 = System.DateTime.Now.ToLongDateString (); T3=2016 Year September 18
string t4 = System.DateTime.Now.ToLongTimeString (); T4=18:14:26
"Fishing and fish" c#string. Format Format Description WriteLine formatting string