格式项都采用如下形式:
{index[,alignment][:formatString]}
其中"index"指索引占位符,这个肯定都知道;
",alignment"按字面意思显然是对齐方式,以","为标记;
":formatString"就是对输出格式的限定,以":"为标记。
alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。
formatString:由标准或自定义格式说明符组成.
下表是从网上得来:
示例:
// Console.WriteLine 中各种数据格式的输出
Console.WriteLine("{0, 8 :C}", 2); // $2.00
Console.WriteLine("{0, 8 :C3}", 2); // $2.000
Console.WriteLine("{0 :D3}", 2); // 002
Console.WriteLine("{0 :E}", 2); // 2.000000E+000
Console.WriteLine("{0 :G}", 2); // 2
Console.WriteLine("{0 :N}", 2500000.00); // 2,500,00.00
Console.WriteLine("{0 :x4}", 12); // 000c
Console.WriteLine("{0, 2 :x}", 12); // c
Console.WriteLine("{0 :000.000}", 12.23); // 012.230
Console.WriteLine("{0 :r}", 15.62); // 15.62
Console.WriteLine("{0 :d}", System.DateTime.Now); // -3-27
Console.WriteLine("{0 :D}", System.DateTime.Now); // 3月27日
Console.WriteLine("{0 :t}", System.DateTime.Now); // 11:43
Console.WriteLine("{0 :T}", System.DateTime.Now); // 11:43:34
Console.WriteLine("{0 :f}", System.DateTime.Now); // 3月27日 11:43
Console.WriteLine("{0 :F}", System.DateTime.Now); // 3月27日 11:43:34
Console.WriteLine("{0 :g}", System.DateTime.Now); // -3-27 11:43
Console.WriteLine("{0 :G}", System.DateTime.Now); // -3-27 11:43:34
Console.WriteLine("{0 :M}", System.DateTime.Now); // 3月27日
Console.WriteLine("{0 :r}", System.DateTime.Now);// Tue, 27 Mar 11:43:34 GMT
Console.WriteLine("{0 :s}", System.DateTime.Now); // -03-27T11:43:34
Console.WriteLine("{0 :u}", System.DateTime.Now); // -03-27 11:43:34Z
Console.WriteLine("{0 :U}", System.DateTime.Now); // 3月27日 3:43:34
Console.WriteLine("{0 :Y}", System.DateTime.Now); // 3月
Console.WriteLine("{0 :dd}", System.DateTime.Now); // 27
Console.WriteLine("{0 :ddd}", System.DateTime.Now); // 二
Console.WriteLine("{0 :dddd}", System.DateTime.Now); // 星期二
Console.WriteLine("{0 :f}", System.DateTime.Now); // 3月27日 11:46
Console.WriteLine("{0 :ff}", System.DateTime.Now); // 18
Console.WriteLine("{0 :fff}", System.DateTime.Now); // 187
Console.WriteLine("{0 :ffff}", System.DateTime.Now); // 1875
Console.WriteLine("{0 :fffff}", System.DateTime.Now); // 18750
Console.WriteLine("{0 :gg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :ggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :gggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :ggggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :gggggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :hh}", System.DateTime.Now); // 11
Console.WriteLine("{0 :HH}", System.DateTime.Now); // 11
Console.WriteLine("{0 :mm}", System.DateTime.Now); // 50
Console.WriteLine("{0 :MM}", System.DateTime.Now); // 03
Console.WriteLine("{0 :MMM}", System.DateTime.Now); // 三月
Console.WriteLine("{0 :MMMM}", System.DateTime.Now); // 三月
Console.WriteLine("{0 :ss}", System.DateTime.Now); // 43
Console.WriteLine("{0 :tt}", System.DateTime.Now); // 上午
Console.WriteLine("{0 :yy}", System.DateTime.Now); // 12
Console.WriteLine("{0 :yyyy}", System.DateTime.Now); //
Console.WriteLine("{0 :zz}", System.DateTime.Now); // +08
Console.WriteLine("{0 :zzz}", System.DateTime.Now); // +08:00
Console.WriteLine("{0 :hh:mm:ss}", System.DateTime.Now); // 11:43:34
Console.WriteLine("{0 :dd/MM/yyyy}", System.DateTime.Now); // 27-03-