From: http://www.cnblogs.com/bignjl/archive/2011/01/14/1935645.html
C # tostring () format string format value: Sometimes, we may need to present the value in a certain format, and we need to format the value. We use a format string to specify the format. The format string adopts the following format: axx, where A is the format specifier, specify the format type, XX is the precision specifier, and control the number of valid digits or decimal places of the formatted output.
Format Description Example output
C currency 2.5.tostring ("C") ¥2.50
D decimal number 25. tostring ("D5") 00025
E science 25000. tostring ("e") 2.500000e + 005
F fixed point 25. tostring ("F2") 25.00
G General 2.5.tostring ("G") 2.5
N number 2500000. tostring ("N") 2,500,000.00
X hexadecimal 255. tostring ("X") FF
Example of string. format in C #
Stringstr1 = string. Format ("{0: N1}", 56789); // results: 56,789.0
Stringstr2 = string. Format ("{0: N2}", 56789); // results: 56,789.00
Stringstr3 = string. Format ("{0: N3}", 56789); // results: 56,789.000
Stringstr8 = string. Format ("{0: F1}", 56789); // results: 56789.0
Stringstr9 = string. Format ("{0: F2}", 56789); // results: 56789.00
Stringstr11 = (56789/100 .0). tostring ("#. #"); // result: 567.89
Stringstr12 = (56789/100). tostring ("#. #"); // result: 567
C or C
Currency
Console. Write ("{0: c}", 2.5); // $2.50
Console. Write ("{0: c}",-2.5); // ($2.50)
D Or d
Decimal number
Console. Write ("{0: D5}", 25); // 00025
E or E
Scientific type
Console. Write ("{0: e}", 250000); // 2.500000e + 005
F or F
Fixed Point
Console. Write ("{0: F2}", 25); // 25.00
Console. Write ("{0: F0}", 25); // 25
G or G
General
Console. Write ("{0: g}", 2.5); // 2.5
N or N
Number
Console. Write ("{0: n}", 2500000); // 2,500,000.00
X or X
Hexadecimal
Console. Write ("{0: x}", 250); // fa
Console. Write ("{0: x}", 0 xFFFF); // FFFF
C # detailed description of the number formatting string 2010-07-03 a.m. a. m. The standard numeric format string is used to format the common numeric type. A string in the standard numeric format is in the form of axx, where A is a letter character known as a format specifier, and XX is an optional integer known as a precision specifier. The range of the precision specifier ranges from 0 to 99, and affects the number of digits in the result. Any numeric string that contains more than one letter character (including space) is interpreted as a custom numeric string.
I found that as long as you search Baidu and Google, articles are the same, excerpted from each other, and copied from each other. There are still some errors, so the code and the code in other articles are verified after running.
Using system;
Namespace tostringformat
{
Class Program
{
Static void main (string [] ARGs)
{
// Convert according to Chinese habits
System. Globalization. cultureinfo culinfo = system. Globalization. cultureinfo. createspecificculture ("ZH-CN ");
// [C or C], which is a string that represents the monetary amount. The conversion is controlled by the currency format information of the current numberformatinfo object.
// The precision specifier indicates the number of decimal places required. If the precision specifier is omitted, the default currency precision given by the current numberformatinfo object is used.
Console. writeline ("(2.5). tostring ('C'):" + (2.5). tostring ("C", culinfo); // ¥2.5
Console. writeline ("(2.5). tostring ('c2 '):" + (2.5). tostring ("C2", culinfo); // ¥2.50
Console. writeline ("(2.5). tostring ('c3'):" + (2.5). tostring ("C3", culinfo); // ¥2.500
Console. writeline ("************************************* ************************");
// [D or D]. This format is supported only for integer types. 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.
Console. writeline ("(25). tostring ('D'):" + (25). tostring ("D", culinfo); // 25
Console. writeline ("(25). tostring ('d3 '):" + (25). tostring ("D3", culinfo); // 025
Console. writeline ("(25). tostring ('d4 '):" + (25). tostring ("D4", culinfo); // 0025
Console. writeline ("(25). tostring ('d5 '):" + (25). tostring ("D5", culinfo); // 00025
Console. writeline ("************************************* ************************");
// [E or E], 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.
Console. writeline ("(25000). tostring ('E'):" + (25000). tostring ("e", culinfo); // 2.500000e + 004
Console. writeline ("(25000). tostring ('E2'):" + (25000). tostring ("E2", culinfo); // 2.50e + 004
Console. writeline ("(25000). tostring ('e3 '):" + (25000). tostring ("E3", culinfo); // 2.500e + 004
Console. writeline ("(25000). tostring ('E'):" + (25000). tostring ("e", culinfo); // 2.500000e + 004
Console. writeline ("(25000). tostring ('E2'):" + (25000). tostring ("E2", culinfo); // 2.50e + 004
Console. writeline ("(25000). tostring ('e3 '):" + (25000). tostring ("E3", culinfo); // 2.500e + 004
Console. writeline ("************************************* ************************");
// [F or F], convert the number 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 value accuracy is given by the numberdecimaldigits attribute of the current numberformatinfo object.
Console. writeline ("(25). tostring ('f0 '):" + (25). tostring ("F0", culinfo); // 25
Console. writeline ("(25). tostring ('f1 '):" + (25). tostring ("f1", culinfo); // 25.0
Console. writeline ("(25). tostring ('F2'):" + (25). tostring ("F2", culinfo); // 25.00
Console. writeline ("************************************* ************************");
// [G or G], based on the number type and whether there is a precision specifier, the number is converted to the most compact form of the 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.
// If the number is decimal and the precision specifier is omitted, an exception exists in the preceding rule. In this case, the fixed-point notation is always used and the tail zero is retained.
// 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 ".
Console. writeline ("(2.5). tostring ('G'):" + (250). tostring ("g", culinfo); // 250
Console. writeline ("(2.5). tostring ('g1 '):" + (250). tostring ("g1", culinfo); // 3E + 02
Console. writeline ("(2.5). tostring ('g2 '):" + (250). tostring ("G2", culinfo); // 2.5e + 02
Console. writeline ("(2.5). tostring ('g3 '):" + (250). tostring ("G3", culinfo); // 250
Console. writeline ("************************************* ************************");
// [N or N], convert the number to "-D, DDD, DDD ..." Format String, where "-" indicates a negative number (if needed), "D" indicates a number (0-9), and "," indicates a thousands separator between the number groups, ". "indicates the decimal point. The actual negative number mode, number group size, thousands separator, and decimal separator are respectively specified by the numbernegativepattern, numbergroupsizes, numbergroupseparator, and numberdecimalseparator attributes of the current numberformatinfo object.
// The precision specifier indicates the number of decimal places required. If the precision specifier is ignored, the default value accuracy is given by the numberdecimaldigits attribute of the current numberformatinfo object.
Console. writeline ("(250000). tostring ('n'):" + (250000). tostring ("N", culinfo); // 250,000.00
Console. writeline ("(250000). tostring ('n1 '):" + (250000). tostring ("N1", culinfo); // 250,000.0
Console. writeline ("(250000). tostring ('n3 '):" + (250000). tostring ("N3", culinfo); // 250,000.000
Console. writeline ("************************************* ************************");
// [X or X] This format is supported only for integer types. 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.
Console. writeline ("(25). tostring ('x'):" + (25). tostring ("X", culinfo); // 19
Console. writeline ("(25). tostring ('x4 '):" + (25). tostring ("X4", culinfo); // 19
Console. writeline ("(25). tostring ('x8'):" + (25). tostring ("X8", culinfo); // 19
Console. writeline ("************************************* ************************");
// [P or p] According to numberformatinfo .. ::. percentnegativepattern attribute (if the number is negative) or numberformatinfo .. ::. the percentpositivepattern attribute (if the number is positive) converts a number to a string that represents the percentage. 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 the current numberformatinfo object is used.
Console. writeline ("(0.025). tostring ('P'):" + (0.025). tostring ("P", culinfo); // 2.50%
Console. writeline ("(0.025). tostring ('p1'):" + (0.025). tostring ("p1", culinfo); // 2.5%
Console. writeline ("(0.025). tostring ('p2'):" + (0.025). tostring ("p2", culinfo); // 2.50%
Console. writeline ("(0.025). tostring ('p3 '):" + (0.025). tostring ("P3", culinfo); // 2.500%
Console. writeline ("************************************* ************************");
// [R or R] Only Single and Double types support this format. 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 there can be a precision specifier, it will be ignored. When this specifier is used, the round-trip process takes precedence over precision.
Console. writeline ("(25.00). tostring ('R'):" + (25.00). tostring ("r", culinfo); // 25
Console. writeline ("(25.00). tostring ('r1'):" + (25.00). tostring ("R1", culinfo); // 25
Console. writeline ("(25.00). tostring ('r2 '):" + (25.00). tostring ("R2", culinfo); // 25
Console. readkey ();
}
}
}
Time Format
Datetime dt = datetime. now; string date; Date = DT. tostring ("D", datetimeformatinfo. invariantinfo); response. write (date + "</BR>"); // 07/22/2009 date = DT. tostring ("g", datetimeformatinfo. invariantinfo); response. write (date + "</BR>"); // 07/22/2009 14:54:37
Date = DT. tostring ("r", datetimeformatinfo. invariantinfo); response. write (date + "</BR>"); // wed, 22 Jul 2009 14:54:37 GMT date = DT. tostring ("T", datetimeformatinfo. invariantinfo); response. write (date + "</BR>"); // 14:54:37 date = DT. tostring ("u", datetimeformatinfo. invariantinfo );
Response. write (date + "</BR>"); // 2009-07-22 14: 54: 37z date = DT. tostring ("DD-mm-YYYY", datetimeformatinfo. invariantinfo); response. write (date + "</BR>"); // 22-07-2009