Converting Other Data Types to String Data
First, keep in mind that the formatting functions I introduced you to in the previous section will return a string value. So, if you use something like this:
Dim iNumber, sString
iNumber = 5
sString = FormatPercent(iNumber, 2)
MsgBox sString
variable sString will contain a string value, because that's what FormatPercent() returns. Technically, the formatting functions are a sort of specialized string conversion function, too.
Dim dDate, sString
VBScript does provide a general string conversion function: CStr(). This function simply takes any type of data-numeric, date/time, currency, or whatever-and converts it to a string. The function works by taking each character of the input data and appending it to an output string. So the number 5 will become "5," the number -2 will become "-2," and so forth. Dates and times are converted to their short display format. For example, try running this.
dDate = Date()
sString = CStr(dDate)
MsgBox sString
The result should be a short formatted date, such as "5/26/2003."
NOTE
If your computer is displaying short dates with a two-digit year, you probably have an outdated version of the Windows Script Host or an incredibly old operating system. All newer versions of Windows and the Windows Script Host display four-digit years to help eliminate future recurrences of the infamous "Y2K bug."
|