VBScript: Converting and Formatting with Functions

In our last tutorial, I tried to cover as many of the Conversion and Formatting functions of VBScript as possible. As it turned out, I only got halfway through the Conversion functions. This time, I will get through the rest of the Conversion functions, and maybe even get to the Formatting functions.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
May 27, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

In fact, this time, I vow to get through at least the rest of the Conversions, or my name isn't Big James Stud. It's a lot to cover, and I really want my name to stay Big James Stud, so let's stop all the yapping and get to tapping. On the keyboard.

Here's the table I know you missed so badly:



Function

What It Does

Type of Function

Asc

Used to convert the first letter in a string to ANSI

Conversion

CBool

Used to convert an expression to Boolean

Conversion

CByte

Used to convert an expression to a Byte

Conversion

CCur

Used to convert an expression to a Currency

Conversion

CDate

Used to convert an expression to a Date

Conversion

CDbl

Used to convert an expression to a Double

Conversion

Chr

Used to convert an ANSI Code to a character

Conversion

CInt

Used to convert an expression to an Integer

Conversion

CLng

Used to convert an expression to a Long

Conversion

CSng

Used to convert an expression to a Single

Conversion

CStr

Used to convert an expression to a String

Conversion

Hex

Used to retrieve the hexadecimal value of a number

Conversion

Oct

Used to retrieve the octal value of a number

Conversion

FormatCurrency

Used to return and format an expression as currency

Format

FormatDateTime

Used to return and format an expression as a date or time

Format

FormatNumber

Used to return and format an expression as a number

Format

FormatPercent

Used to return and format an expression as a percent

Format


The Function with Character

If you need to convert a specific ANSI code to a character, you can use the Chr() function to do so. Here are some examples:


<html>

<head>

<script type="text/vbscript">

document.write(Chr(32) & "<br />")

document.write(Chr(33) & "<br />")

document.write(Chr(34) & "<br />")

document.write(Chr(35) & "<br />")

document.write(Chr(36) & "<br />")

document.write(Chr(37) & "<br />")

document.write(Chr(38) & "<br />")

document.write(Chr(39) & "<br />")

document.write(Chr(40) & "<br />")

document.write(Chr(41) & "<br />")

document.write(Chr(42) & "<br />")

document.write(Chr(43) & "<br />")

document.write(Chr(44) & "<br />")

document.write(Chr(45) & "<br />")

document.write(Chr(46) & "<br />")

document.write(Chr(47) & "<br />")

document.write(Chr(48) & "<br />")

document.write(Chr(49) & "<br />")

document.write(Chr(50) & "<br />")

document.write(Chr(51) & "<br />")

document.write(Chr(52) & "<br />")

document.write(Chr(53) & "<br />")

document.write(Chr(54) & "<br />")

document.write(Chr(55) & "<br />")

document.write(Chr(56) & "<br />")

document.write(Chr(57) & "<br />")

document.write(Chr(58) & "<br />")

document.write(Chr(59) & "<br />")

document.write(Chr(60) & "<br />")

document.write(Chr(61) & "<br />")

document.write(Chr(62) & "<br />")

document.write(Chr(63) & "<br />")

document.write(Chr(64) & "<br />")

document.write(Chr(65) & "<br />")

document.write(Chr(66) & "<br />")

document.write(Chr(67) & "<br />")

document.write(Chr(68) & "<br />")

document.write(Chr(69))

</script>

</head>

<body>

</body>

</html>

This long-winded code will give the following result:

  !
  "
  #
  $
  %
  &
  '
  (
  )
  *
  +
  ,
  -
  .
  /
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
  :
  ;
  <
  =
  >
  ?
  @
  A
  B
  C
  D
  E 

Okay, so maybe I shouldn't have added in the breaks. Here is a smarter example:


<html>

<head>

<script type="text/vbscript">

document.write(Chr(71))

document.write(Chr(105))

document.write(Chr(118))

document.write(Chr(101))

document.write(Chr(32))

document.write(Chr(65))

document.write(Chr(108))

document.write(Chr(108))

document.write(Chr(32))

document.write(Chr(84))

document.write(Chr(111))

document.write(Chr(32))

document.write(Chr(77))

document.write(Chr(69))

document.write(Chr(33))

document.write(Chr(33))

document.write(Chr(33))

</script>

</head>

<body>

</body>

</html>

Again, here is the result of our chatty code:

Give All To ME!!!

Express Your In(teger) Self

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

CSng, the Loneliest Function of them All!

Again, to convert an expression to a single, use this member of the lonely hearts club:


<html>

<head>

<script type="text/vbscript">

dim this, that, theOther

this=900.23

that=800.63

theOther=this*that+(this+that)

document.write(CSng(theOther))

</script>

</head>

<body>

</body>

</html>

Here we have the same result as before, except that the value is now a single:

  722452

I Have a Secret...I CStr

The CStr() function is used to convert an expression to a string, whether that expression is a boolean, numeric, empty (but not null), or date. If it returns a date, keep in mind that it will be formatted as a short-date format:



<html>

<head>

<script type="text/vbscript">

dim value, when, huh

value=#12:10:59 AM#

when=#Jan 12, 1998#

huh=19

document.write(CStr(value)) & "<br />"

document.write(CStr(when)) & "<br />"

document.write(CStr(huh))

</script>

</head>

<body>

</body>

</html>

The result:

  12:10:59 AM
  1/12/1998
  19

Witchy Woman Put a Hex On You

This little guy, the Hex() function, is used to return a string representing the hexadecimal value of a number. If you don't know what hexadecimal is, then pack up your computer and ship it to me, because you don't deserve it:


<html>

<head>

<script type="text/vbscript">

dim a

a=9000

document.write(Hex(8) & "<br />")

document.write(Hex(a) & "<br />")

document.write(Hex(10) & "<br />")

document.write(Hex(100) & "<br />")

document.write(Hex(1000) & "<br />")

document.write(Hex(10000) & "<br />")

document.write(Hex(100000) & "<br />")

document.write(Hex(1000000) & "<br />")

document.write(Hex(10000000))

</script>

</head>

<body>

</body>

</html>

Here is the result:

  8
  2328
  A
  64
  3E8
  2710
  186A0
  F4240
  989680

Oct()...The Last of the Conversion Functions

This final conversion function, the Oct() (I could have totally made a Doctor Octopus joke here) is similar to the Hex() except it returns an octal value. Here it is at work on the same values as before:


<html>

<head>

<script type="text/vbscript">

dim a

a=9000

document.write(Oct(8) & "<br />")

document.write(Oct(a) & "<br />")

document.write(Oct(10) & "<br />")

document.write(Oct(100) & "<br />")

document.write(Oct(1000) & "<br />")

document.write(Oct(10000) & "<br />")

document.write(Oct(100000) & "<br />")

document.write(Oct(1000000) & "<br />")

document.write(Oct(10000000))

</script>

</head>

<body>

</body>

</html>

The result is:

  10
  21450
  12
  144
  1750
  23420
  303240
  3641100
  46113200

Do my eyes deceive me? Or is there actually time left to cover the Formatting Functions? Here they are, all four, in one breathtaking coding example:


<html>

<head>

<script type="text/vbscript">

document.write(FormatCurrency(500)) & "<br />"

document.write("The date is: ")

document.write(FormatDateTime(Date())) & "<br />"

document.write(FormatNumber(500)) & "<br />"

document.write(FormatPercent(10/100))

</script>

</head>

<body>

</body>

</html>

This gives us the result of:

  $500.00
  The date is: 2/21/2008
  500.00
  10.00%

Okay, so that isn't exactly everything there is to know about the Formatting functions; I kind of fooled you. But it is a preview of them at their simplest, and you can certainly use them that way. And to be fair, I do promise to show you how to use them in all their glory in a future article.

In our next article or two we will discuss the Math functions that VBScript offers. There's going to be a lot of scary-looking symbols and words floating around, so make sure you wear a safety helmet.

Till then...

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials