例: Format function

Format 関数の使用例をいくつか示します。

' Currency 
' Get monthly revenue and expenses from the user, converting 
' strings to currency. Compute and display the balance,
' formatted as currency.
Dim rev As Currency, expense As Currency, bal As Currency
rev@ = CCur(InputBox("How much did we make this month?"))
expense@ = CCur(InputBox("How much did we spend?"))
bal@ = rev@ - expense@
MessageBox "Our balance this month is " _
   & Format(bal@, "Currency")




' Percent
Dim total As Integer, attend As Integer, percent As Double
total% = CInt(InputBox("How many people registered?"))
attend% = CInt(InputBox("How many people actually attended?"))
percent# = attend% / total%
MessageBox "The attendance was " _		' Use "Percent" format
	& Format(percent#, "Percent")
MessageBox "The attendance was " _		' Use custom format codes
	& Format(percent#, "0%")		' can carry % to any number of places:
								'  0.0%, 0.00%, 0.000%, and so on




' Example of custom formatting using sections
Dim x As Integer
x = 1
Print Format(x, "0.0;0%")	' Output: 1.0
x = -1
Print Format(x, "0.0;0%")	' Output: 100%
x = 0
Print Format(x, "0.0;0%;zippo")	' Output: zippo
Print chr$(34) & Format(x, "zippo") & chr$(34)	' Output: "zippo"




' Yes/No, True/False, & On/Off
Dim value As Integer
value = 0
Print Format$(value, "Yes/No")		' Output "No"
Print Format$(value, "On/Off")		' Output "Off"
Print Format$(value, "True/False")	' Output "False"
value = 2
Print Format$(value, "Yes/No")		' Output "Yes"
Print Format$(value, "On/Off")		' Output "On"
Print Format$(value, "True/False")	' Output "True"




' Date and Time formats
x = 36525
Print Format(x, "General Date")		' Output: 12/31/1999
Print Format(x, "Long Date")		' Output: Friday, December 31, 1999
Print Format(x, "Medium Date")		' Output: 31-Dec-99
Print Format(x, "Short Date")		' Output: 12/31/99
y = 123.45
Print Format(y, "Long Time")		' Output: 10:48:00 AM
Print Format(y, "Medium Time")		' Output: 10:48 AM
Print Format(y, "Short Time")		' Output: 10:48




' Custom String formats
Dim x As String, y As String, z As String

' Format directory string
x = "dtemp"
Print Format(x, "!copy to directory @:¥¥@")
' Output: copy to directory d:¥temp
'Use sections and NULL data
Print Format(y, "!copy to directory @:¥¥@;don't copy - no valid data")
'Output: don't copy - no valid data

' Manipulate character positions
dim x as string, y as string, z as string
x = "hello world"
y = "hello"
z = "world"
Print Format(x, "!@")				' Output: hello world
print format(y, "&&&&&&&&&&" & z)
' Output: "helloworld" (& ignores non-characters)
print format(y, "!&&&&&&&&&&" & z)
' Output: "helloworld" (! matters not with &)
print format(y, "@@@@@@@@@@" & z)	' Output: "     helloworld"
print format(y, "!@@@@@@@@@@" & z)	' Output: "hello     world"

' Format all characters to lower then upper case
z = "Hello World"
Print Format(z, "<")		' Output: hello world
Print Format(z, ">")		' Output: HELLO WORLD