Click or drag to resize

Val Function

X#
Convert a string containing a numeric value to a numeric data type.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.19
Syntax
 FUNCTION Val(
	cNumber AS STRING
) AS USUAL
Request Example View Source

Parameters

cNumber
Type: String
The string to convert. cNumber can be in any of the compiler-supported base formats, such as binary, decimal, hex, or scientific. When in scientific format, a decimal point is necessary to identify the base "E". So Val("1E+1") equals 1 but Val("1.0E+1") equals 10.0. Moreover, "+INF" and "-INF" (both must be in uppercase) represent +infinity and -infinity.

Return Value

Type: Usual
cNumber as a numeric value.
Remarks
If cNumber is a valid numeric expression, Val() processes it all. However, if cNumber is not entirely a valid numeric expression but does contain decimal numerics, Val() evaluates it until it reaches a second decimal point, the first non-numeric character, or the end of the expression. Leading spaces are always ignored. All variables containing a FLOAT type have internal picture information (like a template) relating to digits and decimals (see FloatFormat()).
For data base fields of type FLOAT, such picture information resides in the database header structure and is obtainable by FieldVal(). With the Val() function and with literal floats (that is, hard-coded in source code and thus known at compile-time), the number of decimals is derived from the hard-coded decimal portion and the number of digits is taken from the SetDigit() or SET DIGIT setting. Str() and Transform() are closely related to Val() since each converts numeric values to strings.
Examples
This example compares the results when SetFixed() is TRUE and when it is FALSE:
X#
1FUNCTION Start()
2    SetDecimal(3)
3    SetFixed(FALSE)
4    ? Val("12.1234")        // 12.1234
5    SetFixed(TRUE)
6    ? Val("12.1234")        // 12.123
7    RETURN TRUE
These examples illustrates Val() with SetFixed() set to TRUE and SetDecimal() set to 2:
X#
1SetDecimal(2)
2SetFixed(TRUE)
3? Val("12.1234")            // 12.12
4? Val("12.1256")            // 12.13
5? Val("12A12")                // 12.00
6? Val("A1212")                // 0.00
7? Val(Space(1))            // 0.00
8? Val(" 12.12")            // 12.12
9? Val("12 .12")            // 12.00
This example applies Val() on a hexadecimal string:
X#
1? Val("0xFF")                // 255
See Also