VAL function  

Purpose

Convert a text string to a numeric value.

Syntax

y = VAL(string_expression [, offset])

Remarks

The VAL function converts a string argument to a number.  If the optional Offset parameter is included, it indicates the position in the string where the conversion should begin.  If not given, it defaults to one (1), and begins at the first character.  Leading white-space characters (spaces, tabs, carriage-returns, and linefeeds) are skipped and ignored.  Evaluation of the number continues until a non-numeric character is found, or the end of the string is reached. If no number is found, the VAL() function returns zero (0).  Format characters (like commas) are not allowed, and will cause early termination of the evaluation.

VAL interprets the letters "e" and "d" (and "E" and "D") as the symbols for exponentiation and scientific notation:

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 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 the string_expressio 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$, DEC$, FORMAT$, and USING$ functions to convert numeric values into decimal strings.  Use BIN$, HEX$ and OCT$ to convert them to Binary, Hexadecimal, and Octal representations.

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$, DEC$, FORMAT$, HEX$, OCT$, STR$, USING$, VAL statement

Example

Price$ = "$ 15,345.92"

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

Result

15345.92