Use of the String. Format () method in C,
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 Aillo
Ii. 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:
N3 and f3 in {0: N2} indicate the type of the 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
This article