In our last article we discussed String, Date, and Math functions. In this article we will cover Conversion and Formatting functions. Since these functions mostly deal with data types, which we've already covered in great detail, I won't spend a lot of time on each one. There are a lot of them, however, and I will cover as many of them as I can in this article.
Contributed by James Payne Rating: / 4 May 19, 2008
The Asc() is used to return the ANSI code for the first character in an expression. Here we use it to display the ANSI code for the letters a through f, both capitalized and lower case:
<html>
<body>
<script type="text/vbscript">
document.write(Asc("A") & "<br />")
document.write(Asc("a") & "<br />")
document.write(Asc("B") & "<br />")
document.write(Asc("b") & "<br />")
document.write(Asc("C") & "<br />")
document.write(Asc("c") & "<br />")
document.write(Asc("D") & "<br />")
document.write(Asc("d") & "<br />")
document.write(Asc("E") & "<br />")
document.write(Asc("e") & "<br />")
document.write(Asc("F") & "<br />")
document.write(Asc("f"))
</script>
</body>
</html>
This gives us the result:
65 97 66 98 67 99 68 100 69 101 70 102
You will note however that I said it gives us the ANSI code for the first character in an expression, so the following will result in the same output as the above, as it still only takes from that first character and not the entire (in this case) string:
As stated in our nifty table, the CBool function doesn't just sound like "See Bull," it also can be used to convert an expression to a boolean value. Basically, if the value you test is a numeric and not equal to 0, then CBool returns True. If the value is numeric and is equal to 0, the it returns false. Finally, if the value is not numeric at all, you get an error. So don't use it on non-numeric values. Got it chump?
<html>
<body>
<script type="text/vbscript">
dim num, num2, num3, txt2
num=20
num2=5*4
num3=0
txt2="592"
document.write(CBool(num) & "<br />")
document.write(CBool(num2)& "<br />")
document.write(CBool(num3)& "<br />")
document.write(CBool(txt2))
</script>
</body>
</html>
The result:
True True False True
Say we changed the value of txt2 to "Hello." If we had done this, you would either get an error message, or neither True nor False would have been returned.
Take A CByte() Outta Crime
The CByte function takes an expression and returns it as a Byte. Here, in this example, we will work with several decimals and a division to see what result CByte returns:
This bad boy takes an expression and converts it to a currency. If it has more than four places after the decimal point...well, maybe I'd better just let you see for yourself. Behold the code and the wondrous result!
<html>
<body>
<script type="text/vbscript">
dim num, num2, num3
num=.03356
num2=12.59633256
num3=0.995
num4= 9/2
document.write(CCur(num) & "<br />")
document.write(CCur(num2) & "<br />")
document.write(CCur(num3)& "<br />")
document.write(CCur(num4))
</script>
</body>
</html>
You will note that while these values are now currency, they don't have a dollar symbol. That's because the computer doesn't need that symbol to tell it the value is a dollar. Only you do. Here is the result:
0.0336 12.5963 0.995 4.5
I Want to CDate() Angeline Jolie
Okay, so I lied. I don't want to date her. I mean, I would have to donate my meager earnings to charitable causes and adopt a bunch of children who would think I was an enormous cheeseburger anyway.
CDate does what all of these other functions do, except it changes the expression (if applicable at any rate) to a date or time format. Here it is in code people:
<html>
<body>
<script type="text/vbscript">
a="April 22, 1977"
document.write(CDate(a))
</script>
</body>
</html>
This results in my birth date:
4/22/1977
Here is another example:
<html>
<body>
<script type="text/vbscript">
a="April 22, 1977"
b=#4/22/77#
c="10:19:50 PM"
d=#12:00:00#
document.write(CDate(a)) & "<br />"
document.write(CDate(b))& "<br />"
document.write(CDate(c))& "<br />"
document.write(CDate(d))
</script>
</body>
</html>
This gives us the results:
4/22/1977 4/22/1977 10:19:50 PM 12:00:00 PM
And lastly, we can use if statements to ensure that the value is, in fact, a date:
<html>
<body>
<script type="text/vbscript">
a="April 22, 1977"
b="Higgeldy Piggeldy"
if IsDate(a) then
document.write(CDate(a)) & "<br />"
end if
if IsDate(b) then
document.write(CDate(b))& "<br />"
else
document.write("That ain't no date sucka!")
end if
</script>
</body>
</html>
This program tests the value in the variable "a" and prints it out if it is a date. It then checks the value in the variable "b" and prints it out if it is a date. If it isn't, then it prints out some text, giving us this result:
This will return the value as a double, resulting in:
1.89809098019811E+23
Though to be fair, even if you tried to print that number without using CDbl() it would print with scientific notation.
Conclusion
Well, I did not get through even half of the functions. But never fear; that just means I'll have to write another article. Or two. Either way, be sure to come back often as we continue and one day, in the far, far off future, finish our description of the VBScript Functions.