C # type conversion, character format conversion and other

xiaoxiao2021-03-19  204

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 string2002-12-10T22: 11: 29uUniversal sortable, local time2002-12-10 22: 13: 50ZUUniversal sortable, GMTDecember 11, 2002 3:13:50 AMYYear month patternDecember, 2002The 'U' specifier seems broken; that string certainly isn ' t sogable.

Custom Date Formatting:

SpecifierTypeExample Example OutputddDay {0: dd} 10dddDay name {0: ddd} TueddddFull day name {0: dddd} Tuesdayf, ff, ... Second fractions {0: fff} 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} DecemberssSeconds 00- 59 {0: SS} 46ttam or PM {0: TT} PMYYEAR, 2 DIGITS {0: YY} 02YYYYYYEAR {0: YYYY} 2002zztimezone Offset, 2 digits {0: zz} -05zzster {0: zz }t {0: zzz} -05 : 00: Separator {0: HH: mm: ss} 10: 43: 20 / separator {0: DD / mm / yyyy} 10/12/2002

ENUMERATIONS

Specifiertypegdefault (Flag Names if Available, OtherWise Decimal) Fflags Alwaysdinteger Alwaysxeight Digit HEX.

Some Useful Examples

String.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: (###) ### - ####}", 8005551212);

THIS WILL OUTPUT "(800) 555-1212".

. If you have any questions about string formatting, or pretty much anything related, drop by the Forum and ask it there It's an easier place to talk than in the comments section here reference link:. (1) http: //www.mcuchina .com / Article / Computer / Programme / Csharp / 200411 / 116.html (2) http://www.113317.com/blog/Article.asp?id=225

转载请注明原文地址:https://www.9cbs.com/read-130244.html

New Post(0)