Q(uick)BASIC Function: VAL

Quick View

VAL

A string-processing function that returns the numeric value of a string of digits

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • STR$(numeric-expression)
  • VAL(stringexpression$)
Description/Parameter(s)
numeric-expression Any numeric expression.
stringexpression$ A string representation of a number.
Example
PRINT "Decimal 65 is represented in hexadecimal as "; PRINT "&H" + LTRIM$(STR$(41)) PRINT VAL(RIGHT$("Microsoft 1990", 4))
Syntax
  • VAL(stringexpression)
Description/Parameter(s)

The stringexpression is a sequence of characters that can be interpreted as a numeric value. The VAL function stops reading the string at the first character that it cannot recognize as part of a number. The VAL function also strips leading blanks, tabs, and line feeds from the argument string. For example,

  • VAL("    -33/LP")

returns the value -33.

Example

The following program prints the names and addresses of people with specific telephone area codes.

' Example program for the VAL function ' ' ***This part of the program builds a sample data file ' OPEN "PHONE.DAT" FOR OUTPUT AS #1 CLS RESTORE READ FuName$, ACPhone$ I = 0 DO WHILE UCASE$(FuName$) <> "END" I = I + 1 WRITE #1, FuName$, ACPhone$ READ FuName$, ACPhone$ IF FuName$ = "END" THEN EXIT DO LOOP CLOSE #1 ' DATA "Bob Hartzell ","206-378-3223" DATA "Alice Provan ","213-884-9700" DATA "Alex Landow ","213-456-3111" DATA "Walt Riley ","503-248-0048" DATA "Georgette Gump ","213-222-2222" DATA "END",0,0,0,0,0 ' ' *** This part of the program demonstrates the VAL function ' INPUT "Search for which area (206, 213, or 503): ", Targetarea OPEN "PHONE.DAT" FOR INPUT AS #1 DO WHILE NOT EOF(1) INPUT #1, Nm$, Phonenum$ 'VAL reads everything up to the first non-numeric 'character ("-" in this case). Area = VAL(Phonenum$) IF Area = Targetarea THEN PRINT PRINT Nm$; PRINT Phonenum$ END IF LOOP CLOSE KILL "PHONE.DAT" END
Syntax
  • VAL(stringexpression$)
Description/Parameter(s)
  • The argument stringexpression$ is a sequence of characters that can be interpreted as a numeric value.

  • The VAL function stops reading the string at the first character that it cannot recognize as part of a number. The VAL function also strips blanks, tabs, and line feeds from the argument string. For example, the code below returns the value 1615198

  • VAL("    1615 198th Street")
Example

This example uses the VAL function to determine the numeric value of numbers stored as strings. The program prints the names and addresses of people with specific telephone area codes.

'This part of the program builds a sample data file. OPEN "PHONE.DAT" FOR OUTPUT AS #1 CLS RESTORE READ FuName$, ACPhone$ I = 0 DO WHILE UCASE$(FuName$) <> "END" I = I + 1 WRITE #1, FuName$, ACPhone$ READ FuName$, ACPhone$ IF FuName$ = "END" THEN EXIT DO LOOP CLOSE #1 DATA "Bob Hartzell ","206-378-3223" DATA "Alice Provan ","213-884-9700" DATA "Alex Landow ","213-456-3111" DATA "Walt Riley ","503-248-0048" DATA "Georgette Gump ","213-222-2222" DATA "END",0,0,0,0,0 'This part of the program demonstrates the VAL function. INPUT "Search for which area (206, 213, or 503): ", Targetarea OPEN "PHONE.DAT" FOR INPUT AS #1 DO WHILE NOT EOF(1) INPUT #1, Nm$, Phonenum$ 'VAL reads everything up to the first non-numeric 'character ("-" in this case). Area = VAL(Phonenum$) IF Area = Targetarea THEN PRINT PRINT Nm$; PRINT Phonenum$ END IF LOOP CLOSE KILL "PHONE.DAT" END