Click or drag to resize

Val Function

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

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.07
Syntax
 FUNCTION Val(
	cNumber AS STRING
) AS USUAL
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#
FUNCTION Start() 
        SetDecimal(3)
        SetFixed(FALSE)
        ? Val("12.1234")                // 12.1234
        SetFixed(TRUE)
        ? Val("12.1234")                // 12.123
        RETURN TRUE
These examples illustrates Val() with SetFixed() set to TRUE and SetDecimal() set to 2:
X#
SetDecimal(2)
SetFixed(TRUE)
? Val("12.1234")                        // 12.12
? Val("12.1256")                        // 12.13
? Val("12A12")                                // 12.00
? Val("A1212")                                // 0.00
? Val(Space(1))                        // 0.00
? Val(" 12.12")                        // 12.12
? Val("12 .12")                        // 12.00
This example applies Val() on a hexadecimal string:
X#
? Val("0xFF")                                // 255
See Also