资源描述:
《Format 函数用法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、格式化数值结果表字符说明示例输出C货币string.Format("{0:C3}",2)$2.000D十进制string.Format("{0:D3}",2)002E科学计数法1.20E+0011.20E+001G常规string.Format("{0:G}",2)2N用分号隔开的数字string.Format("{0:N}",250000)250,000.00X十六进制string.Format("{0:X000}",12)Cstring.Format("{0:000.000}",12.2)012.200StringsTherereallyisn'tanyformattingwithi
2、nastrong,beyondit'salignment.AlignmentworksforanyargumentbeingprintedinaString.Formatcall.SampleGeneratesString.Format("->{1,10}<-","Hello");->Hello<-String.Format("->{1,-10}<-","Hello");->Hello<-NumbersBasicnumberformattingspecifiers:SpecifierTypeFormatOutput(PassedDouble1.42)Output(PassedInt-12
3、400)cCurrency{0:c}$1.42-$12,400dDecimal(Wholenumber){0:d}System.FormatException-12400eScientific{0:e}1.420000e+000-1.240000e+004fFixedpoint{0:f}1.42-12400.00gGeneral{0:g}1.42-12400nNumberwithcommasforthousands{0:n}1.42-12,400rRoundtrippable{0:r}1.42System.FormatExceptionxHexadecimal{0:x4}System.c
4、f90FormatExceptionCustomnumberformatting:SpecifierTypeExampleOutput(PassedDouble1500.42)Note0Zeroplaceholder{0:00.0000}1500.4200Padswithzeroes.#Digitplaceholder{0:(#).##}(1500).42 .Decimalpoint{0:0.0}1500.4 ,Thousandseparator{0:0,0}1,500Mustbebetweentwozeroes.,.Numberscaling{0:0,.}2Commaadjacentt
5、oPeriodscalesby1000.%Percent{0:0%}150042%Multipliesby100,adds%sign.eExponentplaceholder{0:00e+0}15e+2Manyexponentformatsavailable.;Groupseparatorseebelow Thegroupseparatorisespeciallyusefulforformattingcurrencyvalueswhichrequirethatnegativevaluesbeenclosedinparentheses.Thiscurrencyformattingexam
6、pleatthebottomofthisdocumentmakesitobvious:DatesNotethatdateformattingisespeciallydependantonthesystem'sregionalsettings;theexamplestringsherearefrommylocallocale.SpecifierTypeExample(PassedSystem.DateTime.Now)dShortdate10/12/2002DLongdateDecember10,2002tShorttime10:11PMTLongtime10:11:29PMfFullda
7、te&timeDecember10,200210:11PMFFulldate&time(long)December10,200210:11:29PMgDefaultdate&time10/12/200210:11PMGDefaultdate&time(long)10/12/200210:11:29PMMMonthdaypatternDecember10rRFC1123datestringTue,10Dec200222:11:29GM