System.Val
Delphi
procedure Val(S: String; var V; var Code: Integer);
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
procedure | public | System.pas | System | System |
Description
Converts a string that represents an integer (decimal or hex notation) into a number.
In Delphi code, Val converts the string value S
to its numeric representation, as if it were read from a text file with Read. Both $1234 and 0x1234 are the hexadecimal notations supported.
S
is a string-type expression; it must be a sequence of characters that form a signed real number, such as "1", "-2" or "+3".
V
can be an integer-type or real-type variable.
- If
V
is an integer-type (integer, Int64, etc.) variable, thenS
, all characters must be digits; decimal or thousands separators are not supported. - If
V
is a real-type (Single, Double, etc.) variable, thenS
, the string, can contain the decimal or thousands separators.
Code is a variable of type Integer.
If the string is invalid, the index of the offending character is stored in Code; otherwise, Code is set to zero. For a null-terminated string, the error position returned in Code is one larger than the actual zero-based index of the character in error.
Example
procedure Test_Val;
var
LFval: Double;
LIVal: Integer;
LCode: Integer;
begin
// Using integral type
Val('1234', LIVal, LCode); // Valid
if LCode = 0 then
WriteLn('LIVal = ', LIVal);
Val('123.456', LIVal, LCode); // Not valid
if LCode <> 0 then
WriteLn('LCode = ', LCode);
// Using floating type
Val('1234', LFVal, LCode); // Valid
if LCode = 0 then
WriteLn('LFVal = ', LFVal);
Val('123.456', LFVal, LCode); // Valid
if LCode = 0 then
WriteLn('LFVal = ', LFVal);
end;
See Also