procedure Val (const Source: String; var x: integer_or_real; var ErrorPosition: Integer);
Val converts the integer or real number that is represented by the characters in the string Source and places it into x.
The Source string can have a base prefix ($ for hexadecimal or Base#). The optional ErrorCode will be set to the position of the first invalid character, or to a 0 if the entire string represents a valid number. In case an invalid character occurrs in Source, x will be undefined.
Val is a Borland Pascal extension.
program ValDemo; var x, ec: Integer; l: LongInt; r: Real; begin Val ('123', x, ec); { x := 123; ec := 0; } Val ('-123', x, ec); { x := -123; ec := 0; } Val ('123.456', r, ec); { r := 123.456; ec := 0; } Val ('$ffff', x, ec); { x := 65535; ec := 0; } Val ('$F000', x, ec); { x := 61440; ec := 0; } Val ('-$ffff', x, ec); { x := -65535; ec := 0; } Val ('12#100', x, ec); { x := 144; ec := 0; } Val ('-2#11111111', x, ec); { x := -255; ec := 0; } { here we have the invalid character 'X' for base 16 } Val ('$fffeX', x, ec); { x := <undefined>; ec := 6; } Val ('12#100invalid', x, ec); { x := <undefined>; ec := 7; } Val ('36#Jerusalem', l, ec); { l := 54758821170910; ec := 0; } end.