在.net用String.Format可以幫助我們將輸出的資料格式化
例如今天我要把字串變數syear的output左邊補0時
syear = "93"
String.Format("{0:000}", CType(syear, Integer))
輸出的結果:"093"
.net的Format方法提供了很多的指定格式
除了轉數字、金錢、日期之外,還有我們自訂的格式。
數字格式化的specifier如下:
specifier
type
format
output
(double 1234.56)
0
zero placeholder
{0:00.000}
1234.560
#
digit placeholder
{0:#.##}
1234.56
.
decimal point placeholder
{0:0.0}
1234.6
,
thousand separator
{0:0,0}
1,235
%
percentage
{0:0%}
123456%
日期的輸出格式化
Dim d As DateTime = DateTime.Now
Console.WriteLine ("{0}", "ddd", d.ToString ("ddd", null) )
'輸出的結果:Sat
Console.WriteLine ("{0}", d.ToString ("MMMM dd, yyyy", null) )
'輸出的結果:October 17, 2000