Variable Inspection and Numeric Conversion
Part:
1
2
3
(Continued from previous part...)
There are a number of interesting notes here:
- Line V3 shows that (3.14) is reported as (Double). I was expecting Single.
- Line V6 shows that (TRUE) is reported as (Num=Y). What number TRUE will be converted to?
- Line S1 shows that ("777") is reported as (String) and (Num=Y). That is reasonable.
- Line S2 shows that ("3.14") is reported as (String), (Num=Y) and (Date=Y). What date "3.14" will be converted to?
- Line A3 shows that a dynamic-size array is not Empty even before calling ReDim to set its size.
- Lines A1 to A5 show that arrays will never be Empty or Null.
- Lines E1 to E2 show that Empty and Null are independent data types and data literals.
- Line E2 shows that Empty is reported as (Num=Y). What number Empty will be converted to?
- Line O1 shows that (document) is an object of "HTMLDocument" type. This object is created by IE and passed
the IE VB script environment.
Parsing Long Integers
A common task of handling user input is to convert user entered data into a long integer. This sounds like
an easy task. But not really, if you allow the user to type in anything. I have tried my best in the following
example to convert the input data as much as possible into a long integer, parsing_integer.html:
<html>
<body>
<!-- parsing_integer.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
document.writeln(" ")
document.writeln("Converting variants to integers:")
document.writeln(" 777 = " & GetInteger(777))
document.writeln(" ""777"" = " & GetInteger("777"))
document.writeln(" 3.14159 = " & GetInteger(3.14159))
document.writeln(" ""3.14159"" = " & GetInteger("3.14159"))
document.writeln(" ""Hello"" = " & GetInteger("Hello"))
document.writeln(" True = " & GetInteger(True))
document.writeln(" Empty = " & GetInteger(Empty))
document.writeln(" Null = " & GetInteger(Null))
document.writeln(" ""1+2"" = " & GetInteger("1+2"))
document.writeln(" 777777 = " & GetInteger(777777))
document.writeln(" 3.3e200 = " & GetInteger(3.3e200))
document.writeln(" ""3.3e20000"" = " & GetInteger("3.3e20000"))
Function GetInteger(vAnyThing)
If IsNumeric(vAnyThing) Then
' GetInteger = CInt(vAnyThing) 'Error 1: overflow on 777777
' GetInteger = CLng(vAnyThing) 'Error 2: overflow on 3.3e200
GetInteger = CDbl(vAnyThing)
If Abs(GetInteger) < 2147483647 Then
GetInteger = CLng(GetInteger)
Else
GetInteger = -2147483648
End If
Else
GetInteger = -2147483648
End If
End Function
</script>
</pre>
</body>
</html>
Here is the output:
Converting variants to integers:
777 = 777
"777" = 777
3.14159 = 3
"3.14159" = 3
"Hello" = -2147483648
True = -1
Empty = 0
Null = -2147483648
"1+2" = -2147483648
777777 = 777777
3.3e200 = -2147483648
"3.3e20000" = -2147483648
Note that:
- I used IsNumeric() to filter out all other data that could not be converted into a number.
- Then I used CDbl() to get the converted number.
- Comments marked with "Error 1" and "Error 2" show that using "CInt()" or "CLng()" may result
overflow error.
- Converting a Double number to Long number is relatively easy as shown in my code.
Conclusions
- "TypeName()" and "VarType()" are powerful tools to help you to determine the data structure
and data type of any given variable.
- "IsNumeric() and CDbl()" are also useful to convert any given variable to a number.
Part:
1
2
3
|