Understanding Numeric Data in VBScript - Converting data types
(Page 5 of 5 )
Now that you have a better understanding of how number systems work—and how they are treated by VBScript—we can begin to discuss how to convert between different data types as well as how they are handled and evaluated in expressions.
Converting between data types is known as casting. VBScript will do this automatically in most cases; however, it can lead to some unexpected results. It is a much better practice to make explicit casts using the conversion functions provided by VBScript.
Asc(string) 'Returns the ANSI character code corresponding to
the first letter in a string
CBool(expression) 'Returns an expression converted to type Boolean
CByte(expression) 'Returns an expression converted to type Byte
CCur(expression) 'Returns an expression converted to type Currency
CDate(date) 'Returns an expression converted to type Date
CDbl(expression) 'Returns an expression converted to type Double
Chr(charcode) 'Returns the character associated with a specified
ANSI character code
CInt(expression) 'Returns an expression converted to type Integer
CLng(expression) 'Returns an expression converted to type Long
CSng(expression) 'Returns an expression converted to type Single
CStr(expression) 'Returns an expression as a string
Each of the “C” functions, with exception of Chr() and CStr(), is used for casting. Note that if you cast to a narrowing type, some loss of precision will occur. For example, if you cast a double such as 32,450.32 to an Integer, your result will be 32,450. Precision will be preserved, however, when casting to a wider subtype.
The Asc and Chr functions are used to handle ANSI characters. The Chr function returns a character represented by a given ANSI character code while Asc returns the character code for a given character.
MyChar = "F"
MyCharCode = "35"
Asc(MyChar) 'Returns 70
Chr(MyCharCode) ‘Returns "#"
Use caution when building expression that are designed to be evaluated mathematically. If you mix data types in a mathematical expression, VBScript will convert all values to the widest type before evaluating the expression. The expression will return the widest type.
MyInt = 10
MyDouble = 25.32
MyResult = MyInt + MyDouble
WScript.Echo MyInt & " + " & MyDouble & " = " & MyResult
WScript.Echo TypeName(MyInt) & " + " & TypeName(MyDouble) & " = " _
& TypeName(MyResult)
This example demonstrates a calculation using mixed data subtypes. The narrower Integer is first converted to the wider Double type before VBScript makes the calculation. The result is the wider type—Double.
Let’s take a few moments to discuss the String subtype. Strings are a string of ANSI characters. (Or Unicode, if that is the system default.) These are human readable bit patterns consisting of 2-bit character codes for each character in the string. That being said, the CStr function returns some unusual values in some cases.
If expression is | CStr returns |
Boolean | A String containing True or False. |
Date | A String containing a date in the short-date format of your system. |
Null | A run-time error. |
Empty | A zero-length String (""). |
Error | A String containing the word Error followed by the error number. |
Other numeric | A String containing the number. |
The final data types to consider are the special Currency and Date subtypes. These are used for performing calculations with values of the given types.
MyDate1 = #12 / 25 / 00#
MyDate2 = #12 / 1 / 00#
MyResult = MyDate1 - MyDate2 'Returns 24
The expression above returns the number of days between the given dates. In this case, that number is 24. Notice how I’ve used the # notation to indicate that my values are dates. Since this returns a Date object, I could also use any of the available date functions on my returned value.
CCur(1.25 + 1.9545678) 'Returns 3.2046
This example demonstrates a currency calculation. Notice how the result is rounded up to represent thousandths of a whole dollar.
Finally, I’d like to take a minute to revisit the floating point data types that I showed you previously. VBScript provides two functions that allow you to convert (or round) them into integer types.
Int(number)
Fix(number)
Both the Int and Fix functions allow you to remove the decimal portion of a floating point number (by rounding down to the nearest integer). They differ is how they do this with negative numbers.
MyNumber = Int(99.8) ' Returns 99.
MyNumber = Fix(99.2) ' Returns 99.
MyNumber = Int(-99.8) ' Returns -100.
MyNumber = Fix(-99.8) ' Returns -99.
MyNumber = Int(-99.2) ' Returns -100.
MyNumber = Fix(-99.2) ' Returns -99.
The Int function returns the nearest integer less than the given number while the Fix function returns the nearest integer greater than the given number. Another way of looking at this is that Int will always round down to the nearest integer while Fix simply removes the decimal portion leaving the integer portion unchanged.
Now you’ve learned about the different number systems available in VBScript and how they affect the different data subtypes. This understanding should enable you to make more accurate calculations while allowing you to handle all different types of data in your script.
If this seems like a lot to take in, that’s because it is. Take the time to read through this article again and you’ll be more apt to understand the concepts I’ve presented. Take your time, experiment with the different conversion functions to see what they return, and remember to have fun.
Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |