Liberty BASIC Help Online

Numbers and Strings
Liberty BASIC has several functions that convert numeric values and strings.
 
VAL(stringExpression)
Description:
This function returns a numeric value for stringExpression if stringExpression represents a valid numeric value or if it begins with a valid numeric value.  If not, then zero is returned. 
 
Usage:
 
  print 2 * val("3.14")         Produces:       6.28
 
  print val("hello")            Produces:       0
 
  print val("3 blind mice")     Produces:       3
 
 
STR$( numericExpression )
Description:
This function returns a string expressing the result of numericExpression. 
 
Usage:
 
age = 23
age$ = str$(age)
price = 2.99
price$ = str$(price)
totalApples = 37
print "Total number of apples is " + str$(totalApples)
 
USING(templateString, numericExpression)
Description:
This function formats numericExpression as a string using templateString.  The rules for the format are similar to those in Microsoft BASIC's PRINT USING statement,  but since using( ) is a function, it can be used as part of a larger BASIC expression instead of immediate output onlyThe template string consists of the character "#" to indicate placement for numerals, and a single dot "." to indicate placement for the decimal point.  The template string must be contained within double quotation marks.  If there are more digits contained in a number than allowed for by the template string, the digits will be truncated to match the template.
 
A template string looks like this:
 
amount$ = using("######.##", 1234.56)
 
As part of a larger expression:
 
notice "Your total is $" + using("####.##", 1234.5)
 
 
A template string can be expressed as a string variable:
 
template$ = "######.##"
amount$ = using(template$, 1234.56)
 
 
Using() may be used in conjunction with 'print'.  The following two examples produce the same result:
 
amount$ = using("######.##", 123456.78)
print amount$
 
print using("######.##", 123456.78)
 
The using() function for Liberty BASIC 3 has been modified so that it rounds its output like PRINT USING does in other BASICs.
 
Usage:
 
' print a column of ten justified numbers
for a = 1 to 10
    print using("####.##",  rnd(1)*1000)
next a
 
'sample output from the routine above:
  72.06
244.28
133.74
  99.64
813.50
529.65
601.19
697.91
   5.82
619.22
 
HEXDEC( "value" )
Description:
Returns a numeric decimal from a hexadecimal number expressed in a string.  Hexadecimal values are represented by digits 0 - F. the hexadecimal number can be preceded by the characters "&H". The hexadecimal string must be enclosed in quote marks.
 
Usage:
 
    print hexdec( "FF" )
 
or:
 
    print hexdec( "&HFF")
 
 
DECHEX$( number )
Description:
Returns a string representation of a decimal number converted to hexadecimal (base 16)
 
Usage:
 
    print dechex$( 255 )
 
prints...
 
    FF
 
EVAL$(code$) and EVAL(code$)
Description:
Liberty BASIC now has two functions for evaluating BASIC code inside a running program.  The eval() function evaluates the code and returns a numeric value, and the eval$() function works the same way but returns a string value.  Both will execute the very same code, but the string function converts the result to a string if it isn't already one, and the numeric version of the function converts it to numeric values.
Evaluating to a string
Here we show how to evaluate code to a string, and what happens if you try to evaluate it to be a number.
    'Let's evaluate some code that produces a non-numeric result
    a$(0) = "zero"
    a$(1) = "one"
    a$(2) = "two"
    code$ = "a$(int("+str$(rnd(1))+"*3))"
    print "We will evaluate the code: "; code$
    result$ = eval$(code$)
    print result$
 
    'Now let's use the eval function, which effectively does a
    'val() to the result of the calculation.  Converting a non
    'numeric string to a numeric value results in zero.
    result = eval(code$)
    print result
 
Evaluating to a number
Here's an example of the most common type of code evaluation users will want to do: Numeric computation.  Let's just make a short example that asks you to type an expression to evaluate.
    'ask for an expression
    input "Type a numeric expression>"; code$
    answer = eval(code$)
    print answer
 
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/