first, the definition
String.Format is the text equivalent of replacing each format item in the specified String type data with the value of the corresponding object.
Such as:
(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 effect is the same. Replaces the first item's {0} and {1}, respectively, with the values of the last two items.
The result of the output is: Hello Jackie, I ' m Aillo
Second, the String.Format of the multi-format definition:
Here the so-called multi-format refers to a format item can be defined in each of the format parameters, each format parameter separated by a semicolon (;). The value of a format item with 2 and 3 format parameters must be of a numeric type in order to determine whether it is negative, positive, zero.
With 1 format parameters:
//output in the format of scientific notation
Double P1 = 1000000;
Response.Write (String.Format ("{0:e2}", p1));
With 2 format parameters:
/* When the format item corresponds to a non-negative value, select the first format; a negative number selects 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;}", p2));
With 3 format parameters:
/
* Select the first format when the corresponding value for the format item is a positive number;
The negative number is the second medium format;
The value equals zero is the third format */
1double 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));
Add:
{0:N2}The N3,F3 in indicates the type of data after formatting and the number of decimal places. such as: N2 is a number with 2 decimals;
Similar to this:
N or n represents a number
F or f means fixed point
E or E means scientific notation.
D or D denotes decimal number
x or x indicates hexadecimal
G or G means regular
C or C denotes currency
Reprinted from: https://www.ezloo.com/2008/11/string_format.html
String.Format method in C # enter the journal title