您的位置:首页 > 编程语言 > C#

Console.WriteLine()函数中{}输出格式详解(C#)

2010-04-12 16:19 411 查看
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.000
D十进制string.Format("{0:D3}", 2)002
E科学计数法1.20E+0011.20E+001
G常规string.Format("{0:G}", 2)2
N用分号隔开的数字string.Format("{0:N}", 250000)250,000.00
X十六进制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,400
dDecimal (Whole number){0:d}System.
FormatException
-12400
eScientific{0:e}1.420000e+000-1.240000e+004
fFixed point{0:f}1.42-12400.00
gGeneral{0:g}1.42-12400
nNumber with commas for thousands{0:n}1.42-12,400
rRound trippable{0:r}1.42System.
FormatException
xHexadecimal{0:x4}System.
FormatException
cf90
SpecifierTypeExample (Passed System.DateTime.Now)
dShort date10/12/2002
DLong dateDecember 10, 2002
tShort time10:11 PM
TLong time10:11:29 PM
fFull date & time December 10, 2002 10:11 PM
FFull date & time (long)December 10, 2002 10:11:29 PM
gDefault date & time10/12/2002 10:11 PM
GDefault date & time (long)10/12/2002 10:11:29 PM
MMonth day patternDecember 10
rRFC1123 date stringTue, 10 Dec 2002 22:11:29 GMT
sSortable date string2002-12-10T22:11:29
uUniversal sortable, local time2002-12-10 22:13:50Z
UUniversal sortable, GMTDecember 11, 2002 3:13:50 AM
YYear month patternDecember, 2002
SpecifierTypeExample Example Output
ddDay{0:dd}10
dddDay name{0:ddd}Tue
ddddFull day name{0:dddd}Tuesday
f, ff, ...Second fractions{0:fff}932
gg, ...Era{0:gg}A.D.
hh2 digit hour{0:hh}10
HH2 digit hour, 24hr format{0:HH}22
mmMinute 00-59{0:mm}38
MMMonth 01-12{0:MM}12
MMMMonth abbreviation{0:MMM}Dec
MMMMFull month name{0:MMMM}December
ssSeconds 00-59{0:ss}46
ttAM or PM{0:tt}PM
yyYear, 2 digits{0:yy}02
yyyyYear{0:yyyy}2002
zzTimezone offset, 2 digits{0:zz}-05
zzzFull timezone offset{0:zzz}-05:00
:Separator{0:hh:mm:ss}10:43:20
/Separator{0:dd/MM/yyyy}10/12/2002

CSDN中例子:

string myFName = "Fred";
string myLName = "Opals";
int myInt = 100;
string FormatFName = String.Format("First Name = |{0,10}|", myFName);
string FormatLName = String.Format("Last Name = |{0,10}|", myLName);
string FormatPrice = String.Format("Price = |{0,10:C}|", myInt);
Console.WriteLine(FormatFName);
Console.WriteLine(FormatLName);
Console.WriteLine(FormatPrice);

FormatFName = String.Format("First Name = |{0,-10}|", myFName);
FormatLName = String.Format("Last Name = |{0,-10}|", myLName);//-10指左对齐,10个宽度,
FormatPrice = String.Format("Price = |{0,-10:C}|", myInt);//C指以货币形式输出
Console.WriteLine(FormatFName);
Console.WriteLine(FormatLName);
Console.WriteLine(FormatPrice);

//运行结果
//First Name = |          Fred|
//Last Name = |         Opals|
//Price = |           $100.00|
//First Name = |Fred      |
//Last Name = |Opals     |
//Price = |$100.00   |


还有其他事例见http://msdn.microsoft.com/en-us/library/aakt1eab.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: