c # string format conversion

The output of the multiplication tables

 1 int a, b;
 2             string str;
 3             for (a = 1; a <= 9; a++)
 4             {
 5                 for(b=1;b<=a;b++)
 6                 {
 7                     str = string.Format("{0}x{1}={2,-2} ",b,a,a*b);
 8                     Console.Write(str);
 9                 }
10                 Console.WriteLine();
11             }
Line 7 represents the multiplication formula turn into a string format
= STR String .Format ( " {0}. 1 {X} = {2, -2} " , B, A, a * b); // parameters {2, -2} The second parameter representative of the a * b product accounting for two left-justified,

result:

If it changes to:

= STR String .Format ( " {X} {0} = {2,2}. 1 " , B, A, a * b); // {2,2} parameters representative of the second parameter representing the product a * b two right-aligned
Output:

1 string str11 = (56789 / 100.0).ToString("#.#");
2 string str12 = (56789 / 100).ToString("#.#");
3 Console.WriteLine("str11={0},str12={1}.", str11, str12);

Wherein  1 ( 56 789 / 100.0 )  representative of the result of floating type results,  1 ( 56,789 / 100 )  representative of the result to an integer.

 1 String STr11 = ( 56789 / 100.0 ) .ToString ( " # #. " );  On behalf of converting floating-point type, and rounded to one decimal point;

 . 1 String STr11 = ( 56789 / 100 ) .ToString ( " # #. " );  Representative conversion integer type, because it is an integer, the decimal point can be retained even without a defined number of digits decimal point.

Output:

 

In summary, you can see the following examples:

 1 //C#的String.Format举例
 2             string str1 =string.Format("{0:N1}",56789);                //result: 56,789.0
 3             string str2 =string.Format("{0:N2}",56789);                //result: 56,789.00
 4             string str3 =string.Format("{0:N3}",56789);                //result: 56,789.000
 5             string str8 =string.Format("{0:F1}",56789);                //result: 56789.0
 6             string str9 =string.Format("{0:F2}",56789);                //result: 56789.00
 7             string str11 =(56789 / 100.0).ToString("#.##");            //result: 567.89
 8             string str12 =(56789 / 100).ToString(". # ## " );               // Result: 567 
 . 9              // C or c currency 
10              Console.Write ( " {0: C} " , 2.5 );    // $ 2.50 
. 11              Console.Write ( " {0: C} " , - 2.5 ); // ($ 2.50)
 12 is              // d d or decimal number 
13 is              Console.Write ( " {0: D5} " , 25 );    // 00025
 14              // E or e SCIENCES type 
15             Console.Write("{0:E}", 250000);   //2.500000E+005
16             //F 或 f固定点
17             Console.Write("{0:F2}", 25);   //25.00
18             Console.Write("{0:F0}", 25);   //25
19             //G 或 g常规
20             Console.Write("{0:G}", 2.5);   //2.5
21             //N or n number 
22 is              Console.Write ( " {0: N} " , 2500000 );    // 2,500,000.00
 23 is              // X-x or hexadecimal 
24              Console.Write ( " {0: X-} " , 250 ); / / FA

 

The above is the basic type conversion should be noted that information on the position occupied;

Thank you.

Guess you like

Origin www.cnblogs.com/lumao1122-Milolu/p/11489738.html