I. Definition
String. Format Is to replace each format item in the specified string type data with the text equivalent item of the corresponding object value.
For example:
(1)
String p1 = "Jackie ";
String P2 = "aillo ";
Response. Write (string. Format ("Hello {0}, I'm {1}", P1, P2 ));
(2)
Response. Write (string. Format ("Hello {0}, I'm {1}", "Jackie", "aillo "));
The two have the same effect. Both Replace the values of the last two items with the {0} and {1} of the first item respectively }.
The output result is: Hello Jackie, I'm ailloIi. Multi-format definition of string. format:
The so-called multi-format refers to the definition of 1 ~ in a format item ~ Three format parameters. Each format parameter is separated by a semicolon. The value corresponding to the format items with two and three format parameters must be of the numerical type to determine whether it is negative, positive, or zero.
With 1 format parameter:
// Output in scientific notation format
Double p1 = 1000000;
Response. Write (string. Format ("{0: E2}", P1 ));
With two format parameters:
/* If the value corresponding to the format item is not negative, select the first format. If the value is negative, select the second format */
Double p1 = 10000;
Double P2 =-2420.50;
Response. Write (string. Format ("{0 :#,### 0.00 ;#,## 0.000 ;}< br>", P1 ));
Response. Write (string. Format ("{0 :#,### 0.00 ;#,## 0.000 ;}", P2 ));
With three format parameters:
/* Select the first format when the value corresponding to the format item is positive;
The negative number is in the second format;
If the value is equal to zero, the third format */
1 double p1 = 10000;
Double P2 =-2420.50;
Double P3 = 0.00;
Response. write (string. format ("{0 :#,### 0.00 ;#,### 0.000 ;#,### 0.0000 }< br>", P1 ));
Response. write (string. format ("{0 :#,### 0.00 ;#,### 0.000 ;#,### 0.0000 }< br>", P3 ));
Response. Write (string. Format ("{0 :#,### 0.00 ;#,### 0.000 ;#,### 0.0000}", P2 ));
Supplement:
{0: N2}'N3, F3 'indicates the type of formatted data and the number of decimal places. For example, N2 indicates a number with two decimal places;
Similar to this:
N or N indicates a number.
F or F indicates a fixed point
E or e Indicates scientific notation
D or D indicates the decimal number.
X or X indicates hexadecimal
G or G indicates regular
C or C indicates currency
Iformatprovider And . Numberformatinfo provides regional-specific information for formatting numbers of the basic data type, while datetimeformatinfo provides regional-specific information for formatting Date and Time values.