C # format numerical results table
Character Description Example Output C Currency String.Format ("{0: C3}", 2) $ 2.000D Decondition String.Format ("{0: D3}", 2) 002E Scientific Counting Method 1.20e 0011.20e 001g Regular String .Format ("{0: g}", 2) 2N Sring.Format ("{0: N}", 250000) 250,000.00x hex String.Format ("{0: X000 } ", 12) cString.Format (" {0: 000.000} ", 12.2) 012.200
Strings
There real isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument beingprinted in a string.format call.
SamplegeneratsString.format ("-> {1, 10} <-", "hello"); -> hello <-string.format ("-> {1, -10} <-", "hello"); -> Hello <-
Numbers
Basic Number Formatting Specifier:
SpecifierTypeFormat Output (Passed Double 1.42) Output (Passed Int -12400) cCurrency {0: c} $ 1.42- $ 12,400dDecimal (Whole number) {0: d} System.FormatException-12400eScientific {0: e} 1.420000e 000-1.240000e 004fFixed point {0: f} 1.42-12400.00gGeneral {0: g} 1.42-12400nNumber with commas for thousands {0: n} 1.42-12,400rRound trippable {0: r} 1.42System.FormatExceptionxHexadecimal {0: x4} System. Formatexceptioncf90
Custom Number Formatting:
SpecifierTypeExample Output (Passed Double 1500.42) Note0Zero placeholder {0: 00.0000} 1500.4200Pads with zeroes # Digit placeholder. {0: (#) ##.} (1500) .42.Decimal point {0: 0.0} 1500.4, Thousand separator { 0: 0:0,0} 1,500must be between two zeroes.,. Number scaling {0: 0 ,.} 2Comma adjacent to period scales by 1000.% percent {0: 0%} 150042% Multiplies by 100, adds% sign. EEXPONENT PlaceHolder {0: 00e 0} 15e 2MANY Exponent Formats Available.; Group SeparatorSee Below
The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses This currency formatting example at the bottom of this document makes it obvious:. Dates
............................... ...CRIPLILE, TELEGEMENCE.
SpecifierTypeExample (Passed System.DateTime.Now) dShort date10 / 12 / 2002DLong dateDecember 10, 2002tShort time10: 11 PMTLong time10: 11: 29 PMfFull date & time December 10, 2002 10:11 PMFFull date & time (long) December 10, 2002 10:11:29 10:29 Gmtss 2002222: 001:29 Gmtss 200222:11:29 Gmtssable Date 10RRFC1123: 10RRFC112: 11:11:29 gmtssable date, 10, gmtssable date, 10RRFA, TIME, TIME 10/12/2002 10:11:29 pmmmonth day patterndecember Sort: 11: 29uuniversal Sortable, Local Time2002-12-10 22: 13: 50zuuniversal Sortable, GMTDECEMBER 11, 2002 3:13:50 Amyyear Month PatternDecember, 2002
The 'u' Specifier Seem Broken; That String Certainly isn't Sortable.
Custom Date Formatting:
Specifiertypeexample {0: DD} 10dddday name {0: ddd} tueddddddddddddddddddd} tuesdayf, ff, ... second fractions {0: ff} 932GG, ... Era {0: gg} adHH2 digit hour {0: hh} 10HH2 digit hour, 24hr format {0: HH} 22mmMinute 00-59 {0: mm} 38MMMonth 01-12 {0: mM} 12MMMMonth abbreviation {0: MMM} DecMMMMFull month name {0: MMMM } Decembersssseconds 00-59 {0: s} 46ttam or pm {0: tt} PMYYYEAR, 2 DIGITS {0: YY} 02YYYYYEYEAR {0: YYY} 2002zztimezone offset, 2 digits {0: zz} -05zzster Timezone offset {0: ZZZ} -05: 00: Separator {0: hh: mm: ss} 10: 43: 20 / sepator {0: DD / mm / yyyy} 10/12/2002
ENUMERATIONS
Specifiertypegdefault (Flag Names if Available, OtherWise Decimal) Fflags Alwaysdinteger Alwaysxeight Digit HEX.
Some useful exampletring.format ("{0: $ #, ## 0.00; ($ #, ## 0.00); zero}", value);
This Will Output "$ 1,240.00" if passed 1243.50. It will output the Same Format But in Parentheses if The number is negative, and will output the string "zero" if the number is zero.
String.Format ("{0: (###) ### - ####}", 18005551212);
THIS WILL OUTPUT "(800) 555-1212".
Variable.tostring ()
Character-type conversion to string 12345.Tostring ("n"); // Generate 12,345.00 12345.toString ("c"); // Generate ¥ 12,345.00 12345.toTnow ("e"); // Generate 1.234500E 004 12345.Tostring ("f4"); // Generate 12345.0000 12345.toString ("x"); // Generate 3039 (16) 12345.toString ("p"); // Generate 1,234,500.00%