VBScript: Converting and Formatting with Functions - Express Your In(teger) Self
(Page 3 of 5 )
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:
722452
Next: CSng, the Loneliest Function of them All! >>
More Windows Scripting Articles
More By James Payne