BrainDump
  Home arrow BrainDump arrow Page 5 - Understanding Numeric Data in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Understanding Numeric Data in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-26

    Table of Contents:
  • Understanding Numeric Data in VBScript
  • Numeric Subtypes and Precision
  • Number systems
  • Conversion Functions
  • Converting data types

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek