|
Type Conversion
It is often necessary to convert a variable from
one data type to another. CAPITAL Business Script (CBS) provides
functions to perform these conversion tasks.
Character (String) to Number
Conversions
Use the
AsVal()
function to convert a character string to a number. For
example:
Message :=
'123'
Number :=
AsVal(Message)
Number := Number +
1
If a character string does not contain a number,
the AsVal() function will convert
the string to zero. For example, executing AsVal('John') returns the number zero.
Number to Character (String)
Conversions
Use the
Str()
function to convert a number into a character string. The
Str() function accepts two
optional parameters: the length of the string and the number of
decimal places.
Number := 123
Message := Str(Number,
10, 2)
* Result := '
123.00'
Specifying 10 as the second parameter creates a
string that is 10 characters long. The third parameter also
specifies that the character string contain a decimal point and 2
digits. If the third parameter were to be omitted the number would
be rounded to an integer and the returned character string would be
" 123".
If you specify a string length less that is less
than the minimum required to show the full number, CBS returns a
string consisting of a series of asterisks. I.e., "******". This is
a safety precaution that ensures that either a correct number is
displayed or no number at all.
Date to Character and Character to Date
Conversions
Use the function
DTOC()
to convert a date to a character and
CTOD()
to convert a character string to a date. For example:
MyDate :=
CTOD('01/05/01')
* the variable MyDate
now contains a date value
Message :=
DTOC(MyDate)
* the variable Message
now contains the string '01/05/01'
Determining the Type of a Variable
You can determine what type a variable is by
using the
TYPE()
function:
IF Type(MyVar) ==
'C'
RETURN TRUE
Endif
|