converts a string expression into a number.
# = Val(value) - Floating point conversion
# = ValDbl(value) - Floating point conversion
cvar = ValCur(value) - Currency conversion
dvar = ValD(value) - Floating point and/or Date conversion
dvar = ValDate(value) - Floating point and/or Date conversion
% = ValInt(value) - Integer (32 Bit)
Large = ValLarge(value) - Integer (64 Bit)
value:sexp or Date
These functions convert a string or a Date in a numeric value. The type of the return value depends on the Val function used.
Val() and ValDbl() return a floating point value of type Double. ValCur() returns a Currency-value. ValD() and ValDate() return a Date-value (note that the date must be in dd.mm.yy[yy] rather than dd/mm/yyyy format), and ValInt and ValLarge return 32-bit and 64-bit integers respectively.
If during conversion Val() encounters a character which cannot be interpreted as a part of a number ("1234a" for example), the evaluation of the string expression is terminated and the number obtained up until this point (1234 in the above example) is then returned; if the string expression begins with a character which cannot be interpreted as a part of a number, Val() returns 0. The Val?() function can be used to discover how many characters will be converted and, thus, whether all the characters or just some are eligible.
By default, the Val function recognizes the period (.) as a valid decimal separator; however this can be changed by setting the decimal separator using Mode Val: therefore, by using Mode Val "," the comma can be used as a decimal separator instead.
If the string expression begins with &X or %, then binary conversion takes place, &O or &Q converts to octal, while &H, & or $ converts to hexadecimal.
Mode BaseYear sexp sets the year used as base for dates entered with ValD and ValDate. The default (1930) defines annual numbers between "30" and "99" to be interpreted as being from 1930 to 1999 and the values between "00" and "29" as the years 2000 to 2029.
Debug.Show
Trace Val("-.123") // Prints -0.123
Local a$ = Str$(12345) : Trace a$
Trace Val(a$) // Prints 12345
Trace Val("&H" + "AF") // Prints 175
Trace Val("$AA") // Prints 170
Trace Val("%10101011") // Prints 171
Trace ValD("16.09.15") // Prints 12.10.2015
Trace ValD("16/09/15") // Prints 00:00:00 (Only works with German date format)
For examples on using Mode BaseYear and Mode Val see here.
The Val and ValDbl functions don't convert string to numeric values according the regional settings. Instead, Val and ValDbl use the internal GFA-BASIC 32 Mode Val setting. To make sure a program acts according the regional settings use CDbl().
{Created by Sjouke Hamstra; Last updated: 01/08/2022 by James Gaite}