VBScript: Conversion and Format Functions

In our last article we discussed String, Date, and Math functions. In this article we will cover Conversion and Formatting functions. Since these functions mostly deal with data types, which we've already covered in great detail, I won't spend a lot of time on each one. There are a lot of them, however, and I will cover as many of them as I can in this article.

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 4
May 19, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

But first, a table:


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 Asc() Function

The Asc() is used to return the ANSI code for the first character in an expression. Here we use it to display the ANSI code for the letters a through f, both capitalized and lower case:


<html>

<body>

<script type="text/vbscript">

document.write(Asc("A") & "<br />")

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

document.write(Asc("B") & "<br />")

document.write(Asc("b") & "<br />")

document.write(Asc("C") & "<br />")

document.write(Asc("c") & "<br />")

document.write(Asc("D") & "<br />")

document.write(Asc("d") & "<br />")

document.write(Asc("E") & "<br />")

document.write(Asc("e") & "<br />")

document.write(Asc("F") & "<br />")

document.write(Asc("f"))

</script>

</body>

</html>

This gives us the result:

  65
  97
  66
  98
  67
  99
  68
  100
  69
  101
  70
  102

You will note however that I said it gives us the ANSI code for the first character in an expression, so the following will result in the same output as the above, as it still only takes from that first character and not the entire (in this case) string:


<html>

<body>

<script type="text/vbscript">

document.write(Asc("Apple") & "<br />")

document.write(Asc("ape") & "<br />")

document.write(Asc("Banana") & "<br />")

document.write(Asc("baboon") & "<br />")

document.write(Asc("Chinchilla") & "<br />")

document.write(Asc("chaquita") & "<br />")

document.write(Asc("Doscile") & "<br />")

document.write(Asc("digereedoo") & "<br />")

document.write(Asc("Enoch") & "<br />")

document.write(Asc("epoch") & "<br />")

document.write(Asc("Funky") & "<br />")

document.write(Asc("funkay"))

</script>

</body>

</html>

As stated above this gives us the same result:

  65
  97
  66
  98
  67
  99
  68
  100
  69
  101
  70
  102

CBool...CBool Run

As stated in our nifty table, the CBool function doesn't just sound like "See Bull," it also can be used to convert an expression to a boolean value. Basically, if the value you test is a numeric and not equal to 0, then CBool returns True. If the value is numeric and is equal to 0, the it returns false. Finally, if the value is not numeric at all, you get an error. So don't use it on non-numeric values. Got it chump?


<html>

<body>

<script type="text/vbscript">

dim num, num2, num3, txt2

num=20

num2=5*4

num3=0

txt2="592"

document.write(CBool(num) & "<br />")

document.write(CBool(num2)& "<br />")

document.write(CBool(num3)& "<br />")

document.write(CBool(txt2))

</script>

</body>

</html>

The result:

  True
  True
  False
  True

Say we changed the value of txt2 to "Hello." If we had done this, you would either get an error message, or neither True nor False would have been returned.

Take A CByte() Outta Crime

The CByte function takes an expression and returns it as a Byte. Here, in this example, we will work with several decimals and a division to see what result CByte returns:


<html>

<body>

<script type="text/vbscript">

dim num, num2, num3

num=.03356

num2=12.5963

num3=0.995

num4= 9/2

document.write(CByte(num) & "<br />")

document.write(CByte(num2) & "<br />")

document.write(CByte(num3)& "<br />")

document.write(CByte(num4))

</script>

</body>

</html>

The result of this code is:

  0
  13
  1
  4

CCur...Did I Stutter? Oh Yeah, I Guess I Did

This bad boy takes an expression and converts it to a currency. If it has more than four places after the decimal point...well, maybe I'd better just let you see for yourself. Behold the code and the wondrous result!


<html>

<body>

<script type="text/vbscript">

dim num, num2, num3

num=.03356

num2=12.59633256

num3=0.995

num4= 9/2

document.write(CCur(num) & "<br />")

document.write(CCur(num2) & "<br />")

document.write(CCur(num3)& "<br />")

document.write(CCur(num4))

</script>

</body>

</html>

You will note that while these values are now currency, they don't have a dollar symbol. That's because the computer doesn't need that symbol to tell it the value is a dollar. Only you do. Here is the result:

  0.0336
  12.5963
  0.995
  4.5

I Want to CDate() Angeline Jolie

Okay, so I lied. I don't want to date her. I mean, I would have to donate my meager earnings to charitable causes and adopt a bunch of children who would think I was an enormous cheeseburger anyway.

CDate does what all of these other functions do, except it changes the expression (if applicable at any rate) to a date or time format. Here it is in code people:


<html>

<body>

<script type="text/vbscript">

a="April 22, 1977"

document.write(CDate(a))

</script>

</body>

</html>

This results in my birth date:

  4/22/1977


Here is another example:

<html>

<body>

<script type="text/vbscript">

a="April 22, 1977"

b=#4/22/77#

c="10:19:50 PM"

d=#12:00:00#

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

document.write(CDate(b))& "<br />"

document.write(CDate(c))& "<br />"

document.write(CDate(d))

</script>

</body>

</html>

This gives us the results:

  4/22/1977
  4/22/1977
  10:19:50 PM
  12:00:00 PM

And lastly, we can use if statements to ensure that the value is, in fact, a date:


<html>

<body>

<script type="text/vbscript">

a="April 22, 1977"

b="Higgeldy Piggeldy"

if IsDate(a) then

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

end if

if IsDate(b) then

document.write(CDate(b))& "<br />"

else

document.write("That ain't no date sucka!")

end if

</script>

</body>

</html>

This program tests the value in the variable "a" and prints it out if it is a date. It then checks the value in the variable "b" and prints it out if it is a date. If it isn't, then it prints out some text, giving us this result:

  4/22/1977
  That ain't no date sucka!

One final pun...

This Next Part Will Make You CDbl()

Once more, CDbl() does what? It lets you return an expression as a double. Yay! Here is it in code:


<html>

<body>

<script type="text/vbscript">

dim dayamn

dayamn=189809098019810980110981.80098098109810981098

document.write(CDbl(dayamn))

</script>

</body>

</html>

This will return the value as a double, resulting in:

  1.89809098019811E+23

Though to be fair, even if you tried to print that number without using CDbl() it would print with scientific notation.

Conclusion

Well, I did not get through even half of the functions. But never fear; that just means I'll have to write another article. Or two. Either way, be sure to come back often as we continue and one day, in the far, far off future, finish our description of the VBScript Functions.

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 9 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials