Console.WriteLine()函数中{}输出格式详解(C#)
来源:互联网 发布:轴承座型号查询数据 编辑:程序博客网 时间:2024/04/30 23:02
Console.WriteLine()函数的格式一直没怎么注意。今天同事问起Console.WriteLine({0:D3},a)的意义,忽然发现不知道D代表什么意义。以前以为{0,4}是指第一个变量输出时占8位,今天查了一下,发现也并不完全正确。
其中格式项都采用如下形式:
{index[,alignment][:formatString]}
其中"index"指索引占位符,这个肯定都知道;
",alignment"按字面意思显然是对齐方式,以","为标记;
":formatString"就是对输出格式的限定,以":"为标记。
alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。
formatString:由标准或自定义格式说明符组成.
下表是从网上得来:
字符
说明
示例
输出
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)C string.Format("{0:000.000}", 12.2)012.200
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
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 PMgDefault date & time10/12/2002 10:11 PMGDefault date & time (long)10/12/2002 10:11:29 PMMMonth day patternDecember 10rRFC1123 date stringTue, 10 Dec 2002 22:11:29 GMTsSortable date string2002-12-10T22:11:29uUniversal sortable, local time2002-12-10 22:13:50ZUUniversal sortable, GMTDecember 11, 2002 3:13:50 AMYYear month patternDecember, 2002
SpecifierTypeExample Example OutputddDay{0:dd}10dddDay name{0:ddd}TueddddFull day name{0:dddd}Tuesdayf, ff, ...Second fractions{0:fff}932gg, ...Era{0:gg}A.D.hh2 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}PMyyYear, 2 digits{0:yy}02yyyyYear{0:yyyy}2002zzTimezone offset, 2 digits{0:zz}-05zzzFull timezone offset{0:zzz}-05:00:Separator{0:hh:mm:ss}10:43:20/Separator{0:dd/MM/yyyy}10/12/2002
CSDN中例子:
还有其他事例见http://msdn.microsoft.com/en-us/library/aakt1eab.aspx
- Console.WriteLine()函数中{}输出格式详解(C#)
- C#中Console.WriteLine()函数中{}输出格式详解
- C# Console.WriteLine()函数中{}输出格式详解
- C# Console.WriteLine()函数中{}输出格式详解
- C# Console.WriteLine()函数中{}输出格式详解
- C# Console.WriteLine()函数中{}输出格式详解
- C#中Console.WriteLine()函数输出格式详解
- C# Console.WriteLine()函数中{}输出格式详解
- Console.WriteLine 字符串格式
- C# Console.WriteLine()的输出格式控制
- [c#学习]关于Console.WriteLine()格式输出we
- C#第一课中写入标准输出流console.writeline
- Console.WriteLine 需要输出 {} 怎么办
- Console.WriteLine()格式化输出小记
- Console.WriteLine()格式化输出小记
- C#中 使用委托模拟 Console.WriteLine()函数.
- 显示Console.WriteLine输出的内容
- C#控制台 console.writeline输出变量
- python 发email
- 主流java rss
- EXT为文本框控件扩展验证属性
- asp.net 页面跳转方式总结(网摘)
- 国际足球赛事
- Console.WriteLine()函数中{}输出格式详解(C#)
- java伪静态
- 查看session的size
- 关于 DCOM的问题
- VC CSocket文件传送示例
- Javascript刷新框架及页面的方法总集
- message loop and win prok
- “角斗士”乔布斯:我的人生狠字当头
- 让VS2005打开VS2008的解决方案