Difference between revisions of "Str"

From Free Pascal wiki
Jump to navigationJump to search
(rewrite)
Line 1: Line 1:
 
{{Str}}
 
{{Str}}
  
Procedure '''Str''' converts the numeric ([[Real]], [[Integer]], [[Longint]], [[Byte]] or [[Word]]) value to a string representation according to the '''Width''' and '''Decimals''' formatting parameters.
+
The [[Procedure|procedure]] {{Doc|package=RTL|unit=system|identifier=str|text=<syntaxhighlight lang="pascal" inline>str</syntaxhighlight>}} converts an ordinal or [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]] type value to a string representation thereof.
  
X is an integer-type or real-type expression. Width and Decimals are integer-type expressions. S is a string-type variable.
+
== invocation ==
 +
The formal signature of <syntaxhighlight lang="pascal" inline>str</syntaxhighlight> cannot be written in [[Pascal]], hence a description follows:
  
Declaration
+
<syntaxhighlight lang="pascal" inline>Str</syntaxhighlight> requires two parameters.
procedure Str(X [: Width [: Decimals ]]; var S);
+
The first parameter has to be an ordinal or real type expression.
 +
The second parameter specifies the destination.
 +
It has to be of the type [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]] (or an <syntaxhighlight lang="pascal" inline>array[dimension] of char</syntaxhighlight>).
  
See also:
+
The first parameter may optionally be followed by a [[Colon|colon]] and an [[Integer|integer]] expression.
* [[Val]]
+
This will determine the minimum width of the generated string representation.
 +
However, bear in mind if the destination variable is a ''fixed''-length string, no ''more'' characters than its maximum capacity can be stored.
 +
In this case any surplus characters will be clipped.
  
[[Category:Pascal]]
+
If the first parameter is a <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> expression, the parameter may be followed by another colon and integer after the first colon and integer specifying the minimum width.
 +
This number, however, will determine the number of decimal places after the radix mark.
 +
 
 +
== formatting ==
 +
Specifying a second formatting for the <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> number representation will disable the usual scientific notation.
 +
 
 +
Enumerated type values will be left-justified, numeric values will be right-justified.
 +
 
 +
<syntaxhighlight lang="pascal" inline>Str</syntaxhighlight> cannot produce decimal numbers containing a [[Comma|comma]] as a radix mark.
 +
 
 +
<syntaxhighlight lang="pascal" inline>Str</syntaxhighlight> only produces numbers to the base of ten.
 +
[[FPC|FPC’s]] standard [[RTL|run-time library]] provides the functions {{Doc|package=RTL|unit=system|identifier=binstr|text=<syntaxhighlight lang="pascal" inline>binStr</syntaxhighlight>}}, {{Doc|package=RTL|unit=system|identifier=octstr|text=<syntaxhighlight lang="pascal" inline>octStr</syntaxhighlight>}}, and {{Doc|package=RTL|unit=system|identifier=hexstr|text=<syntaxhighlight lang="pascal" inline>hexStr</syntaxhighlight>}}, which produce string representations of integers according to base of two, eight and sixteen respectively.
 +
<syntaxhighlight lang="pascal" inline>Real</syntaxhighlight> values cannot be easily represented in other bases.
 +
 
 +
== see also ==
 +
* {{Doc|package=RTL|unit=system|identifier=writestr|text=<syntaxhighlight lang="pascal" inline>writeStr</syntaxhighlight>}} which does very same operation, but accepts an “infinite” number of arguments
 +
* [[Val|<syntaxhighlight lang="pascal" inline>val</syntaxhighlight>]] which performs the reverse operation

Revision as of 17:40, 31 May 2020

Deutsch (de) English (en) русский (ru)

The procedure str converts an ordinal or real type value to a string representation thereof.

invocation

The formal signature of str cannot be written in Pascal, hence a description follows:

Str requires two parameters. The first parameter has to be an ordinal or real type expression. The second parameter specifies the destination. It has to be of the type string (or an array[dimension] of char).

The first parameter may optionally be followed by a colon and an integer expression. This will determine the minimum width of the generated string representation. However, bear in mind if the destination variable is a fixed-length string, no more characters than its maximum capacity can be stored. In this case any surplus characters will be clipped.

If the first parameter is a real expression, the parameter may be followed by another colon and integer after the first colon and integer specifying the minimum width. This number, however, will determine the number of decimal places after the radix mark.

formatting

Specifying a second formatting for the real number representation will disable the usual scientific notation.

Enumerated type values will be left-justified, numeric values will be right-justified.

Str cannot produce decimal numbers containing a comma as a radix mark.

Str only produces numbers to the base of ten. FPC’s standard run-time library provides the functions binStr, octStr, and hexStr, which produce string representations of integers according to base of two, eight and sixteen respectively. Real values cannot be easily represented in other bases.

see also

  • writeStr which does very same operation, but accepts an “infinite” number of arguments
  • val which performs the reverse operation