VAL function

Purpose

Return the numeric equivalent of a string argument.

Syntax

y = VAL(string_expression)

Remarks

VAL turns a string argument into a number.  If string_expression begins with numeric characters (0 to 9 and +, -, or.), but also contains non-numeric characters, VAL returns the number up to the point of the non-numeric character.  If string_expression does not begin with a numeric character, VAL returns 0.  The string argument should not contain any commas, as VAL terminates processing if a comma is encountered.  Leading white-space characters (spaces, tabs, and linefeed characters) are ignored.

VAL is often used in data entry routines.  With it, a program can prompt the user for numeric data (which is usually retrieved in string form), and then convert the legal portions of the string into a numeric result (see the examples below).

VAL also interprets the letters "e" and "d" (and "E" and "D") as the symbols for exponentiation.  Both "e" and "d" are supported for compatibility with other BASIC languages, but there is no difference in operation between these symbols.  For example:

i& = VAL("10.101e3") ' 10101 ~ 10.101*(10^3)

j& = VAL("2D4")      ' 20000 ~ 2 * (10 ^ 4)

Hexadecimal, Binary and Octal conversions

VAL can also be used to convert string arguments that are in the form of integer-class Hexadecimal, Binary and Octal numbers.  Hexadecimal values should be prefixed with "&H" and Binary with "&B".  Octal values may be prefixed "&O", "&Q" or just "&".  If string_expression contains a leading zero, the result is returned as an unsigned value; otherwise, a signed value is returned.  For example:

i& = VAL("&HF5F3")       ' Hex, returns -2573 (signed)

j& = VAL("&H0F5F3")      ' Hex, returns 62963 (unsigned)

x& = VAL("&B0100101101") ' Binary, returns 301 (unsigned)

y& = VAL("&O4574514")    ' Octal, returns 1243468 (signed)

Valid hex characters include 0 to 9, A to F (and a to f).  Valid Octal characters include 0 to 7, and binary 0 to 1.

Use the STR$ and FORMAT$ functions to convert numeric values into decimal strings.  Use BIN$, HEX$ and OCT$ to convert numeric values into string representations of Binary, Hexadecimal and Octal number system values.

Restrictions

VAL stops analyzing string_expression when non-numeric characters are encountered.  When dealing with Hexadecimal, Binary, and Octal number systems, the period character is classified as non-numeric.  This is because PowerBASIC only supports floating-point formats for the decimal number system.  VAL accepts the period character as a decimal place for all decimal number system values.

VAL does not analyze trailing type-specifiers for decimal strings.  For example, VAL("9.1&") is evaluated as 9.1 rather than 9 because the "&" suffix is treated as a non-numeric character, not a type-specifier. However, type suffixes may be used with binary, octal, and hex values.

See also

BIN$, FORMAT$, HEX$, OCT$, STR$

Example

Price$ = "$ 15,345.92"

Cost@@ = VAL(REMOVE$(Price$, ANY "$, "))

Result

15345.92