资料格式化输出 -------------------------------------------------------------------------------- ●有网友问:为什么用Format("A:"),VB却告诉我错误,也无法对软碟机格式化? Ans:VB所提供的Format函数并不像DOS的Format有相同的功能,VB的Format函数是把资料包裹成指订的样式输出。 ●用法:传回数组 = Format(资料, 格式) ●简单范例。 Private Sub Command1_Click() Print Format(1234, "0#####") End Sub ●结果Prt打出:"001234" Private Sub Command1_Click() Print Format(1234, "(0#####)") End Sub ●结果Prt打出:"(001234)" Private Sub Command1_Click() Dim TlPrice As Long TlPrice = TtPrice + 430 TlPrice = TtPrice + 120 TlPrice = TtPrice + 230 Print Format(TtPrice, "总价是0#####元") End Sub ●结果Prt打出:"总价是000780元" Private Sub Command1_Click() Dim TlPrice As Long TlPrice = TtPrice + 430 TlPrice = TtPrice + 120 TlPrice = TtPrice + 230 Print Format(TtPrice, "总价是#####0000元") End Sub ●结果Prt打出:"总价是0780元" Private Sub Command1_Click() Dim MyTime As String MyTime ="12:5:6" Print Format(MyTime, "hh:mm:ss") End Sub ●结果Prt打出:"12:05:06" Private Sub Command1_Click() Dim MyTime As String MyTime ="12:5:6" Print Format(MyTime, "AMPM hh:mm:ss") End Sub ●结果Prt打出:"PM 12:05:06" Private Sub Command1_Click() Dim MyTime As String MyTime ="77/10/18" Print Format(MyTime, "倩倩的生日 yyyy/mm/dd") End Sub ●结果Prt打出:"倩倩的生日 1977/10/18" 格式说明 # 显示数字 0 在#前未满#的位数都补0、在#后未满0位数都补0 y 显示年 m 显示月或分 d 显示日 h 显示时 s 显示秒 : 时间分隔 / 日期分隔 AMPM 显示是上午或下午
本示例显示用 Format 函数做格式化输出的不同用法。对于日期分隔号(/),时间分隔号(:),以及 AM/ PM 等文本而言,其真正的显示格式会因计算机上的国际标准不同而有所差异。在开发阶段,日期与时间是以短日期的格式,配合代码的国际标准来显示的。而在运行时,短日期则是根据系统的国际标准而定,而系统的国际标准和代码的国际标准可能并不相同。本示例中是假设国际标准为 English/United States。MyTime 及 MyDate 在开发环境下,使用系统的短日期设置显示出来的。Dim MyTime, MyDate, MyStr
MyTime = #17:04:23#
MyDate = #January 27, 1993#' 以系统设置的长时间格式返回当前系统时间。
MyStr = Format(Time, "Long Time")' 以系统设置的长日期格式返回当前系统日期。
MyStr = Format(Date, "Long Date")MyStr = Format(MyTime, "h:m:s") ' 返回 "17:4:23"。
MyStr = Format(MyTime, "hh:mm:ss AMPM") ' 返回 "05:04:23 PM"。
MyStr = Format(MyDate, "dddd, mmm d yyyy") ' 返回 "Wednesday, Jan 27 1993"。
' 如果没有指定格式,则返回字符串。
MyStr = Format(23) ' 返回 "23"。' 用户自定义的格式。
MyStr = Format(5459.4, "##,##0。00") ' 返回 "5,459.40"。
MyStr = Format(334。9, "###0。00") ' 返回 "334.90"。
MyStr = Format(5, "0。00%") ' 返回 "500.00%"。
MyStr = Format("HELLO", "<") ' 返回 "hello"。
MyStr = Format("This is it", ">") ' 返回 "THIS IS IT"。
expression 必要参数。任何有效的表达式。
format 可选参数。有效的命名表达式或用户自定义格式表达式。
firstdayofweek 可选参数。常数,表示一星期的第一天。
firstweekofyear 可选参数。常数,表示一年的第一周。
设置值firstdayofweek 参数有下面设置:常数 值 说明
vbUseSystem 0 使用 NLS API 设置。
VbSunday 1 星期日(缺省)
vbMonday 2 星期一
vbTuesday 3 星期二
vbWednesday 4 星期三
vbThursday 5 星期四
vbFriday 6 星期五
vbSaturday 7 星期六
firstweekofyear 参数有下面设置:常数 值 说明
vbUseSystem 0 使用 NLS API 设置。
vbFirstJan1 1 从包含一月一日的那一周开始(缺省)。
vbFirstFourDays 2 从本年第一周开始,而此周至少有四天在本年中。
VbFirstFullWeek 3 从本年第一周开始,而此周完全在本年中。
说明格式化 作法
数字 使用预先定义的命名数值格式或创建用户自定义数值格式。
日期和时间 使用预先定义的命名日期/时间格式或创建用户自定义日期/时间格式。
日期和时间序数 使用日期和时间格式或数值格式。
字符串 创建自定义的字符串格式。
如果在格式化数字时没有指定 format,Format 会提供与 Str 函数类似的功能,尽管它是国际化的。然而,以 Format 作用在正数上不会保留正负号空间,而以 Str 的话则会。
--------------------------------------------------------------------------------
●有网友问:为什么用Format("A:"),VB却告诉我错误,也无法对软碟机格式化?
Ans:VB所提供的Format函数并不像DOS的Format有相同的功能,VB的Format函数是把资料包裹成指订的样式输出。
●用法:传回数组 = Format(资料, 格式)
●简单范例。
Private Sub Command1_Click()
Print Format(1234, "0#####")
End Sub
●结果Prt打出:"001234"
Private Sub Command1_Click()
Print Format(1234, "(0#####)")
End Sub
●结果Prt打出:"(001234)"
Private Sub Command1_Click()
Dim TlPrice As Long
TlPrice = TtPrice + 430
TlPrice = TtPrice + 120
TlPrice = TtPrice + 230
Print Format(TtPrice, "总价是0#####元")
End Sub
●结果Prt打出:"总价是000780元"
Private Sub Command1_Click()
Dim TlPrice As Long
TlPrice = TtPrice + 430
TlPrice = TtPrice + 120
TlPrice = TtPrice + 230
Print Format(TtPrice, "总价是#####0000元")
End Sub
●结果Prt打出:"总价是0780元"
Private Sub Command1_Click()
Dim MyTime As String
MyTime ="12:5:6"
Print Format(MyTime, "hh:mm:ss")
End Sub
●结果Prt打出:"12:05:06"
Private Sub Command1_Click()
Dim MyTime As String
MyTime ="12:5:6"
Print Format(MyTime, "AMPM hh:mm:ss")
End Sub
●结果Prt打出:"PM 12:05:06"
Private Sub Command1_Click()
Dim MyTime As String
MyTime ="77/10/18"
Print Format(MyTime, "倩倩的生日 yyyy/mm/dd")
End Sub
●结果Prt打出:"倩倩的生日 1977/10/18"
格式说明 # 显示数字
0 在#前未满#的位数都补0、在#后未满0位数都补0
y 显示年
m 显示月或分
d 显示日
h 显示时
s 显示秒
: 时间分隔
/ 日期分隔
AMPM 显示是上午或下午