C
Currency
2.5.ToString ("C")
¥2.50
D
Decimal number
25.ToString ("D5")
00025
E
Scientific type
25000.ToString ("E")
2.500000E+005
F
Fixed point
25.ToString ("F2")
25.00
G
Conventional
2.5.ToString ("G")
2.5
N
Digital
2500000.ToString ("N")
2,500,000.00
X
Hexadecimal
255.ToString ("X")
Ff
Formatcode is an optional format code string. (For more information, search for "format string" view)
Format must be separated from other characters with "{" and "}". If you want to also use curly braces in a format, you can use two consecutive curly braces to represent a brace, that is, "{{" or "}}".
Examples of commonly used formats:
(1) int i=12345;
This.textbox1.text=i.tostring ();
Result 12345 (this refers to the current object, or an instance of the current class)
This.textbox2.text=i.tostring ("D8");
Result 00012345
(2) int i=123;
Double j=123.45;
String s1=string. Format ("The value is {0,7:d}", i);
String s2=string. Format ("The value is {0,7:f3}", j);
THIS.TEXTBOX1.TEXT=S1;
Results The value is 123
THIS.TEXTBOX2.TEXT=S2;
Results The value is 123.450
(3) Double i=12345.6789;
This.textbox1.text=i.tostring ("F2"); Result 12345.68
This.textbox2.text=i.tostring ("F6");
Result 12345.678900
(4) Double i=12345.6789;
This.textbox1.text=i.tostring ("n"); Results 12,345.68
This.textbox2.text=i.tostring ("N4"); Results 12,345.6789
(5) Double i=0.126;
String s=string. Format ("The value is {0:p}", i);
This.textbox1.text=i.tostring ("P"); Result 12.6%
This.textbox2.text=s; Results The value is 12.6%
(6) DateTime dt =new datetime (2003,5,25);
This.textbox1.text=dt. ToString ("yy"). M.D ");
Results 03.5.25
This.textbox2.text=dt. ToString ("yyyy years M");
Results May 2003
(7) int i=123;
Double j=123.45;
String s=string. Format ("i:{0,-7},j:{1,7}", i,j);
-7 represents left-aligned, 7-bit
This.textbox1.text=s;
Results i:123, j:123.45