TYPE CASTING FUNCTIONS



VBScript Type Conversion Functions:

These are the functions which help to convert data from one type to another type

CBool() - Convert to Boolean Type 
Converts an expression to a Boolean variable type that is either True or False.
syntax:
Converts an expression to a Boolean variable type that is either True or False.
Boolean = CBool(Expression)
Returns True or False

Example

Result = CBool(1 + 1 = 5) 'Result is falseResult = CBool(1 + 1 = 2) 'Result is true

CByte() - Convert to Byte Value 
Converts an expression to a Byte type (i.e. an integer value in the range 0 to 255).
Syntax:
Byte = CByte(Expression)
Returns Byte

Example

Dim strTest, byteTest
strTest = "13" 'Set as string valuebyteTest = CByte(strTest) 'Result is a byte

CCur() - Convert to Currency Type 
Converts an expression to a Currency type (i.e. a floating-point number with up to four decimal places).
Syntax:
Currency = CCur(Expression)
Returns Floating-Point Number

Example

strAmount = "$11.50" 'String Amountdocument.write CCur(strAmount) 'Converted to Currency
CDate() - Convert to Date Value 
Converts a formatted date string to a date variable type.
syntax:
Date = CDate(String Date)
Returns Date

Example

strDate = "February 11, 2000" 'Formatted Stringdocument.write CDate(strDate) 'Date Type

CDbl() - Convert to a Double Value 
Converts an expression to a double-precision floating point number.
syntax:

Double = CDbl(Expression)

Returns Floating-Point Number

Example

strNum = "11.5037" 'String Numberdocument.write CDbl(strNum) 'Converted to Double variable 

CInt() - Convert to Integer Value 
Converts an expression into an integer value in the range -32,768 to 32,767.
Syntax:

Integer = CInt(Expression)

Returns Integer Number

Example:

response.write(CInt("300") 

The output will be:
300

CLng()- Convert to Long Integer Value 
Converts an expression to a long integer value.
Syntax:

Long = CLng(Expression)

Returns Long Integer

Example:
response.write(CLng("300000")
output:
300000

CSng() - Convert to a Single Value 
Converts an expression into a single-precision floating-point number.
Syntax:

Single = CSng(Expression)

Returns Floating-Point Number
Example:
response.write(CSng("300000")
response.write(CSng(1536.75263541)
response.write(CSng(-6700000)
output:
300000
1536.753
-6700000

CStr() - Convert to String Value 
Converts an expression into a string value.
Syntax:

String = CStr(Expression)

Returns String
Example:
response.write(CStr("300000")
response.write(CStr(#10-05-25#)
output :
300000
10/5/2025




No comments:

Post a Comment