Difference between revisions of "Val"
(→usage: no localization) |
(+content) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Val}} | {{Val}} | ||
− | The [[Procedure|procedure]] {{Doc|package=RTL|unit=system|identifier=val|text=<syntaxhighlight lang="pascal" | + | The [[Procedure|procedure]] {{Doc|package=RTL|unit=system|identifier=val|text=<syntaxhighlight lang="pascal" inline>system.val</syntaxhighlight>}} attempts to convert a string representation of a numeric value into a numeric value variable. |
− | + | This [[Borland Pascal]] extension is part of the default [[RTL|run-time library]] delivered with the [[FPC|FreePascal compiler]], but otherwise not standardized. | |
== usage == | == usage == | ||
The formal signature reads:<syntaxhighlight lang="pascal">procedure val(const s: string; var v; var code: word)</syntaxhighlight> | The formal signature reads:<syntaxhighlight lang="pascal">procedure val(const s: string; var v; var code: word)</syntaxhighlight> | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>S</syntaxhighlight> is a [[String|string]]-type expression; |
it must be a sequence of characters that form a (possibly signed) number or enumerated type value. | it must be a sequence of characters that form a (possibly signed) number or enumerated type value. | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>V</syntaxhighlight> is an ordinal or [[Real|real]]-type variable. |
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>Code</syntaxhighlight> is a [[Word|word]] integer variable. |
− | If the string <syntaxhighlight lang="pascal" | + | If the string <syntaxhighlight lang="pascal" inline>s</syntaxhighlight> can not be converted to a numeric value, <syntaxhighlight lang="pascal" inline>code</syntaxhighlight> holds the index of the first character causing troubles; |
− | otherwise <syntaxhighlight lang="pascal" | + | otherwise <syntaxhighlight lang="pascal" inline>code</syntaxhighlight> is zero, indicating success. |
− | {{Note|In contrast to <syntaxhighlight lang="pascal" | + | {{Note|In contrast to <syntaxhighlight lang="pascal" inline>read</syntaxhighlight>, the value of <syntaxhighlight lang="pascal" inline>v</syntaxhighlight> remains unchanged if no data (i.e. empty string <syntaxhighlight lang="pascal" inline>s</syntaxhighlight>) is available. |
− | No <syntaxhighlight lang="delphi" | + | No <syntaxhighlight lang="delphi" inline>default</syntaxhighlight> value is assigned.}} |
− | The procedure <syntaxhighlight lang="pascal" | + | The procedure <syntaxhighlight lang="pascal" inline>val</syntaxhighlight> is so useful, since it does not trigger [[runtime error|run-time errors]] or [[Exceptions|exceptions]]. |
− | Unlike [[Read|<syntaxhighlight lang="pascal" | + | Unlike [[Read|<syntaxhighlight lang="pascal" inline>read</syntaxhighlight>]] the user can be supplied with more useful error messages telling which character is not a numeric symbol. |
− | The power of <syntaxhighlight lang="pascal" | + | The power of <syntaxhighlight lang="pascal" inline>val</syntaxhighlight> lies in its capability to also convert enumerated types ''by their named values''. |
<syntaxhighlight lang="pascal" highlight="3,6,11"> | <syntaxhighlight lang="pascal" highlight="3,6,11"> | ||
program valDemo(input, output, stdErr); | program valDemo(input, output, stdErr); | ||
Line 35: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note, as a general design principle this feature should not be used for user responses like this example shows, since it thwarts internationalization. | 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 <syntaxhighlight lang="pascal" | + | Localization in general is not possible with <syntaxhighlight lang="pascal" inline>val</syntaxhighlight>, most notably the decimal separator – in some regions a [[Comma|comma]], in others a [[period|period]] – can not be customized; |
it is always a period, as it is standard in your [[Standard Pascal|Pascal]] source code. | it is always a period, as it is standard in your [[Standard Pascal|Pascal]] source code. | ||
+ | However, <syntaxhighlight lang="pascal" inline>val</syntaxhighlight> still may be useful for reading and parsing ''generated'' data. | ||
== see also == | == see also == | ||
− | * [[Str|<syntaxhighlight lang="pascal" | + | * [[Str|<syntaxhighlight lang="pascal" inline>system.str</syntaxhighlight>]] performs the reverse action |
* [[RTTI]] | * [[RTTI]] | ||
+ | * [https://BorlandPascal.FanDom.com/wiki/String_operations String operations] in ''BorlandPascal Fandom Wiki'' | ||
[[Category:Code]] | [[Category:Code]] |
Latest revision as of 15:52, 7 February 2023
│
Deutsch (de) │
English (en) │
русский (ru) │
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:
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.
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.
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
system.str
performs the reverse action- RTTI
- String operations in BorlandPascal Fandom Wiki