Val Procedure
Val
converts a string to a numeric value. The
Result
argument can be an integer,
Int64
, or floating-point variable. If the
conversion is successful, Code
is zero. Otherwise,
the value of Code
is the string position where
Val
first detected a format error.
Val
is not a real
procedure.
To convert a string to a floating-point number, use the string
conversion functions in the SysUtils
unit instead
of Val
. The problem is that Val
does not heed the local settings for the decimal separator, making
the procedure useless in an international
setting.
// Prompt the user for a number, and return the number that the
// user enters. If the user enters invalid input, show what
// the user mistyped and try again.
function GetNumber(const Prompt: string): Int64;
var
S: string;
Code: Integer;
begin
repeat
Write(Prompt);
ReadLn(S);
Val(S, Result, Code);
if Code <> 0 then
begin
WriteLn(S);
WriteLn('^':Code, ' invalid input'),
end;
until Code = 0;
end;