[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] Reference for unit 'System' (#rtl)


Calculate numerical/enumerated value of a string.


Source position: system.fpd line 49

procedure Val(

  const S: ;

  var V;

  var Code: Word



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 type Longint, Real and Byte or any enumerated type. If the conversion isn't succesfull, 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 conversion to enumerated exists only as of version 2.3.1 (or later) of the compiler.


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



Convert a numerical value to a string.


Program Example74;

{ Program to demonstrate the Val function. }
Var I, Code : Integer;

  Val (ParamStr (1),I,Code);
  If Code<>0 then
    Writeln ('Error at position ',code,' : ',Paramstr(1)[Code])
    Writeln ('Value : ',I);
This page is hosted on Get Lazarus Code and Component Repository at SourceForge.net. Fast, secure and Free Open Source software downloads