Sign in Join
Wiki copytemporary

Val

Deutsch · English · Русский

The procedure system.val attempts to convert a string representation of a numeric value into a numeric value variable. This Borland Pascal extension is part of the default run-time library delivered with the FreePascal compiler, but otherwise not standardized.

usage

The formal signature reads:

pascal
procedure val(const s: string; var v; var code: word)

S is a string-type expression; it must be a sequence of characters that form a (possibly signed) number or enumerated type value. V is an ordinal or real-type variable. Code is a word integer variable.

If the string s can not be converted to a numeric value, code holds the index of the first character causing troubles; otherwise code is zero, indicating success.

Note: In contrast to read, the value of v remains unchanged if no data (i.e. empty string s) is available. No default value is assigned.

The procedure val is so useful, since it does not trigger run-time errors or exceptions. Unlike read the user can be supplied with more useful error messages telling which character is not a numeric symbol. The power of val lies in its capability to also convert enumerated types by their named values.

pascal
program valDemo(input, output, stdErr);
type
    decision = (no, yes, maybe);
var
    s: string;
    r: decision;
    c: word;
begin
    writeLn('Are you OK?');
    readLn(s);
    val(s, r, c);
    writeLn('So ', r, '.');
end.

Note, as a general design principle this feature should not be used for user responses like this example shows, since it thwarts internationalization. Localization in general is not possible with val, most notably the decimal separator – in some regions a comma, in others a period – can not be customized; it is always a period, as it is standard in your Pascal source code. However, val still may be useful for reading and parsing generated data.

see also


From the Free Pascal wiki, page "Val", as it stood on 2023-02-07 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.