Val procedure
Calculate numerical/enumerated value of a string.
Declaration
procedure Val(const S: string; var V; var Code: Word)Description
Val converts the value represented in the string
S to a numerical
value or an enumerated value, and stores this value in the variable
V, which
can be of any integer type (signed or unsigned, sizes from 1 to 8 bytes),
floating point type (of any supported size) or any enumerated type.
If the conversion is not successful, then the parameter
Code contains the index of the character in
S which
prevented the conversion.
The string
S is allowed to contain spaces in the beginning.
The string S can contain a number in decimal, hexadecimal, binary
or octal format, as described in the language reference. For enumerated
values, the string must be the name of the enumerated value. The name is
searched case insensitively.
The string can use various radices in addition to a decimal (radix 10) notation:
-
$,0xorxcan be used for hexadecimal notation. Allowed characters are0..9andA..F(case insensitive) -
&for octal notation. Allowed characters are0..7. -
%for binary notation. Allowed characters are0..1.
The conversion to enumerated exists only as of version 2.3.1 (or later) of the compiler.
If S represents a numerical value in decimal notation, or a negative number,
then the numerical value must fit into the range of the type of V,
otherwise Val will report an error.
If S represents a numerical value in binary, octal or hexadecimal notation,
and does not contain a minus sign, then the numerical value must fit into the unsigned
counterpart of the type of V and will be casted to the type of V. If not, Val will report an error.
Errors
If the conversion doesn't succeed, the value of
Code indicates the
position where the conversion went wrong. The value of
V is then
undefined.
See also
Example: ex74
Program Example74;
{ Program to demonstrate the Val function. }
Var I, Code : Integer;
begin
Val (ParamStr (1),I,Code);
If Code<>0 then
Writeln ('Error at position ',code,' : ',Paramstr(1)[Code])
else
Writeln ('Value : ',I);
end.An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
