String Formate Sample

  • 4263
  • 0
  • 2017-07-27

摘要:String Formate Sample

'Standard Numeric Format Specifiers
"(C) Currency: . . . . . . . . {0:C}"  . ($123.00)                                     
"(D) Decimal:. . . . . . . . . {0:D}"  . -123                                          
"(E) Scientific: . . . . . . . {1:E}"  . -1.234500E+002                                
"(F) Fixed point:. . . . . . . {1:F}"  . -123.45                                       
"(G) General:. . . . . . . . . {0:G}"  . -123                                          
"(N) Number: . . . . . . . . . {0:N}"  . -123.00                                       
"(P) Percent:. . . . . . . . . {1:P}"  . -12,345.00 %                                  
"(R) Round-trip: . . . . . . . {1:R}"  . -123.45                                       
"(X) Hexadecimal:. . . . . . . {0:X}"  . FFFFFF85                                      
                                                                                       
Standard DateTime Format Specifiers          
"(d) Short date: . . . . . . . {0:d}"  . 6/26/2004                                     
"(D) Long date:. . . . . . . . {0:D}"  . Saturday, June 26, 2004                       
"(t) Short time: . . . . . . . {0:t}"  . 8:11 PM                                       
"(T) Long time:. . . . . . . . {0:T}"  . 8:11:04 PM                                    
"(f) Full date/short time: . . {0:f}"  . Saturday, June 26, 2004 8:11 PM               
"(F) Full date/long time:. . . {0:F}"  . Saturday, June 26, 2004 8:11:04 PM            
"(g) General date/short time:. {0:g}"  . 6/26/2004 8:11 PM                             
"(G) General date/long time: . {0:G}"  . 6/26/2004 8:11:04 PM                          
"(M) Month:. . . . . . . . . . {0:M}"  . June 26                                       
"(R) RFC1123:. . . . . . . . . {0:R}"  . Sat, 26 Jun 2004 20:11:04 GMT                 
"(s) Sortable: . . . . . . . . {0:s}"  . 2004-06-26T20:11:04                           
"(u) Universal sortable: . . . {0:u}   . 2004-06-26 20:11:04Z (invariant)              
"(U) Universal sortable: . . . {0:U}"  . Sunday, June 27, 2004 3:11:04 AM              
"(Y) Year: . . . . . . . . . . {0:Y}"  . June, 2004                                    
                                                                                   
Standard Enumeration Format Specifiers   
"(G) General:. . . . . . . . . {0:G}"  . Green                                         
"(F) Flags:. . . . . . . . . . {0:F}   . Green (flags or integer)                      
"(D) Decimal number: . . . . . {0:D}"  . 3                                             
"(X) Hexadecimal:. . . . . . . {0:X}"  . 00000003                                      

Digits after decimal point
// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

Thousands separator
String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67);       // "12,346"

Zero
Following code shows how can be formatted a zero (of double type).

String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0);            // "0"
String.Format("{0:#.0}", 0.0);            // ".0"
String.Format("{0:#.#}", 0.0);            // ""

Align numbers with spaces
String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

Custom formatting for negative numbers and zero
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);  // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0);        // "zero"

Some funny examples
String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3);          // "12aaa.bbb3"

var culture = CultureInfo.GetCultureInfo("en-US");
int amount = 20000 / 1000;
var amountString = string.Format(culture, "{0:C}K", amount);
Console.WriteLine(amountString); // $20.00

double amount = 20000;
string formatted = string.Format("${0:0,.0}K", amount);


string foo = yourDateTime.ToUniversalTime()
                         .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
 


TonyHuang