VBScript: Converting and Formatting with Functions
In our last tutorial, I tried to cover as many of the Conversion and Formatting functions of VBScript as possible. As it turned out, I only got halfway through the Conversion functions. This time, I will get through the rest of the Conversion functions, and maybe even get to the Formatting functions.
Contributed by James Payne Rating: / 1 May 27, 2008
In fact, this time, I vow to get through at least the rest of the Conversions, or my name isn't Big James Stud. It's a lot to cover, and I really want my name to stay Big James Stud, so let's stop all the yapping and get to tapping. On the keyboard.
Here's the table I know you missed so badly:
Function
What It Does
Type of Function
Asc
Used to convert the first letter in a string to ANSI
Conversion
CBool
Used to convert an expression to Boolean
Conversion
CByte
Used to convert an expression to a Byte
Conversion
CCur
Used to convert an expression to a Currency
Conversion
CDate
Used to convert an expression to a Date
Conversion
CDbl
Used to convert an expression to a Double
Conversion
Chr
Used to convert an ANSI Code to a character
Conversion
CInt
Used to convert an expression to an Integer
Conversion
CLng
Used to convert an expression to a Long
Conversion
CSng
Used to convert an expression to a Single
Conversion
CStr
Used to convert an expression to a String
Conversion
Hex
Used to retrieve the hexadecimal value of a number
Conversion
Oct
Used to retrieve the octal value of a number
Conversion
FormatCurrency
Used to return and format an expression as currency
Format
FormatDateTime
Used to return and format an expression as a date or time
Format
FormatNumber
Used to return and format an expression as a number
Format
FormatPercent
Used to return and format an expression as a percent
Got an expression you want converted into an Integer? Well, CInt() is for you! And you can own it for the low, low price of $19.95. All checks payable to James Payne...
<html>
<head>
<script type="text/vbscript">
dim this, that, theOther
this=32766.9892939
that=-32767.99999999999
document.write(CInt(this)) & "<br />"
document.write(CInt(that))
</script>
</head>
<body>
</body>
</html>
This converts our values to integers and prints out:
32767 -32768
Note that the value must be a number and the minimum and maximum amount is between -32,768 and 32,767.
CLng, Nice Knowing You
If you need to convert an expression to a larger number, you can use the CLng function, which looks suspiciously like a Klingon. Not the Original Trek Klingon...the Deep Space Nine one, which is somehow less scary:
<html>
<head>
<script type="text/vbscript">
dim this, that, theOther
this=2147483646.9999999999
that=-2147483647.999999999
document.write(CLng(this)) & "<br />"
document.write(CLng(that))
</script>
</head>
<body>
</body>
</html>
Again, there is a min and max of -2147483648 and 2147483647, respectively. Note that when I say expression, I really mean expression. In all of these samples I have assigned a value to the variable. But nothing is to stop you from doing this as well:
<html>
<head>
<script type="text/vbscript">
dim this, that, theOther
this=900.23
that=800.63
theOther=this*that+(this+that)
document.write(CLng(theOther))
</script>
</head>
<body>
</body>
</html>
Here we performed some equations, adding them to that lonely variable I inserted into the other examples but never used, giving us this result:
Again, to convert an expression to a single, use this member of the lonely hearts club:
<html>
<head>
<script type="text/vbscript">
dim this, that, theOther
this=900.23
that=800.63
theOther=this*that+(this+that)
document.write(CSng(theOther))
</script>
</head>
<body>
</body>
</html>
Here we have the same result as before, except that the value is now a single:
722452
I Have a Secret...I CStr
The CStr() function is used to convert an expression to a string, whether that expression is a boolean, numeric, empty (but not null), or date. If it returns a date, keep in mind that it will be formatted as a short-date format:
This little guy, the Hex() function, is used to return a string representing the hexadecimal value of a number. If you don't know what hexadecimal is, then pack up your computer and ship it to me, because you don't deserve it:
<html>
<head>
<script type="text/vbscript">
dim a
a=9000
document.write(Hex(8) & "<br />")
document.write(Hex(a) & "<br />")
document.write(Hex(10) & "<br />")
document.write(Hex(100) & "<br />")
document.write(Hex(1000) & "<br />")
document.write(Hex(10000) & "<br />")
document.write(Hex(100000) & "<br />")
document.write(Hex(1000000) & "<br />")
document.write(Hex(10000000))
</script>
</head>
<body>
</body>
</html>
Here is the result:
8 2328 A 64 3E8 2710 186A0 F4240 989680
Oct()...The Last of the Conversion Functions
This final conversion function, the Oct() (I could have totally made a Doctor Octopus joke here) is similar to the Hex() except it returns an octal value. Here it is at work on the same values as before:
Do my eyes deceive me? Or is there actually time left to cover the Formatting Functions? Here they are, all four, in one breathtaking coding example:
<html>
<head>
<script type="text/vbscript">
document.write(FormatCurrency(500)) & "<br />"
document.write("The date is: ")
document.write(FormatDateTime(Date())) & "<br />"
document.write(FormatNumber(500)) & "<br />"
document.write(FormatPercent(10/100))
</script>
</head>
<body>
</body>
</html>
This gives us the result of:
$500.00 The date is: 2/21/2008 500.00 10.00%
Okay, so that isn't exactly everything there is to know about the Formatting functions; I kind of fooled you. But it is a preview of them at their simplest, and you can certainly use them that way. And to be fair, I do promise to show you how to use them in all their glory in a future article.
In our next article or two we will discuss the Math functions that VBScript offers. There's going to be a lot of scary-looking symbols and words floating around, so make sure you wear a safety helmet.