VAL statement  

Purpose

Convert a text string to a numeric value with additional information.

Syntax

VAL StrgExpr [, offset] TO ValueVar [, DigitsVar&, UnusedVar&]

Remarks

The VAL statement converts a string argument to a number, but adds additional information about the conversion.  Both Leading and trailing white-space characters (spaces, tabs, carriage-returns, and linefeeds) are skipped and ignored.  If no number is found, the value zero (0) is returned.  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:

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

VAL "2D4" TO j&        ' 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 StrgExpr contains a leading zero, the result is returned as an unsigned value; otherwise, a signed value is returned.  For example:

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

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

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

VAL "&O4574514"  TO y&    ' 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.

Offset

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.

ValueVar

A numeric variable which receives the result of the conversion.

DigitsVar

An optional long integer variable which receives the count of the number of significant digits found in the evaluation.  If this value is zero (0), no valid number was found and zero (0) was also assigned to ValueVar.

UnUsedVar

An optional long integer variable which receives the count of the unused characters.  Since the evaluation skips both leading and trailing white-space, a non-zero value indicates that additional characters of some significance may be present.  You can use RIGHT$(StrgExpr, UnUsedVar) to separate the unused characters.

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 function

Example

s = "The total cost is $145.26."

VAL s, INSTR(s, "$")+1 to i

Result

145.26