VBScript: Conversion and Format Functions - CCur...Did I Stutter? Oh Yeah, I Guess I Did
(Page 4 of 5 )
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...
Next: This Next Part Will Make You CDbl() >>
More Windows Scripting Articles
More By James Payne