VBScript: Conversion and Format Functions - CBool...CBool Run
(Page 3 of 5 )
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:
<html>
<body>
<script type="text/vbscript">
dim num, num2, num3
num=.03356
num2=12.5963
num3=0.995
num4= 9/2
document.write(CByte(num) & "<br />")
document.write(CByte(num2) & "<br />")
document.write(CByte(num3)& "<br />")
document.write(CByte(num4))
</script>
</body>
</html>
The result of this code is:
0
13
1
4
Next: CCur...Did I Stutter? Oh Yeah, I Guess I Did >>
More Windows Scripting Articles
More By James Payne