Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

VAL

Keyword Abbreviation Token (hex) Version(s) Classification
VAL V{Shift+A} C5 1.0+ Function

  Syntax  
VAL string )
 
Parameters Type Legal Value(s) Default Value Note(s)
string String
any
 
Returns Type Value(s) Note(s)
number Floating-point
any
 
  Purpose  
Return a floating-point number specified in a text string.

 
  Remarks  
The VAL function parses the text string much like the interpreter does in direct/immediate mode: any leading spaces are skipped if present and then the characters that make up the number are processed (spaces embedded in the number are also skipped).  It accepts a leading + or - sign, whole numbers, fractional values (such as "1.5", "0.5", or ".5"), or scientific notation (like "1.5e3").  The string will only be interpretted as a decimal number (compare with DEC).  The first non-valid character (in particular, tab, line-feed, comma, and carriage return) ends processing.  Any string supplied will be processed without reporting an error.  If no characters are successfully processed, the function returns zero.
 
The VAL function is useful to process a numeric value aquired by INPUT/GET into a string variable.  This is because string variables are preferred with them to prevent an error from crashing the program should the user accidently (or maliciously) enter a non-numeric value.  Because no string will ever generate an error with VAL, you might want to be cautious when zero is returned.
 
You will get TYPE MISMATCH ERROR with a numeric expression or variable supplied as a "string".
  
Examples:
PRINT VAL(" +3 2,000")
 32
 
READY.
PRINT VAL("2")
 2
 
READY.
PRINT VAL(2)

?TYPE MISMATCH ERROR
READY.
PRINT VAL("A")
 0

READY.
 
  Compare With  
 
  Contrast With  
 
  See Also  

© H2Obsession, 2014
Comments