Sign in Join
Wiki copytemporary

Format function: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The **format** [[Function|function]] formats a series of values into a [[String|string]]. The format function is found in the unit *[[SysUtils unit|SysUtils]]*. For an in-depth explanation see the [`sysutils.format`](https://www.freepascal.org/docs-html/rtl/sysutils/format.html) on line documentation.
22
33## Syntax
44
55Format is overloaded with two ways to call it:
66
77``` pascal
88function Format( const Fmt: String; const Args: array of Const) : String;
99function Format( const Fmt: String; const Args: array of Const; const FormatSettings: TFormatSettings) : String;
1010```
1111
1212The Fmt String argument contains *format specifiers* or *placeholders* (e.g. %d) which correspond to and are replaced in the result string by values from the Args array. An [[Const|array of const]] can hold a [[Variable|variable]] amount of values of different types, with the applicable types for the Format function being the variations of the [[Real]], [[Integer]], [[Pointer]] and [[String]] types. [[TFormatSettings]] is a [[Record|record]] which holds information related to desired formatting of number, time, date and currency values.
1313
1414The formatting rule for each given *format specifier* is:
1515
16-`% [ArgumentIndex:] ['-'] [width] [. precision] ArgumentType`
16+```text
17+% [ArgumentIndex:] ['-'] [width] [. precision] ArgumentType
18+```
1719
1820| | |
1921|-----|-------------------|
2022| d | Decimal (integer) |
2123| e | Scientific |
2224| f | Fixed |
2325| g | General |
2426| m | Money |
2527| n | Number (floating) |
2628| p | Pointer |
2729| s | String |
2830| u | Unsigned decimal |
2931| x | Hexadecimal |
3032
3133### Integer formatting
3234
3335| | |
3436|----|----|
3537|   %d | Will print the integer as it is. |
3638|   %8d | Will print the integer as it is. If the number of digits is less than 8, the output will be padded on the left. |
3739|   %-8d | Will print the integer as it is. If the number of digits is less than 8, the output will be padded on the right. |
3840|   %.8d | Will print the integer as it is. If the number of digits is less than 8, the output will be padded on the left with zeroes. |
3941
4042### String formatting
4143
4244| | |
4345|----|----|
4446|   %s | Will print the string as it is. |
4547|   %8s | Will print the string as it is. If the string has less than 8 characters, the output will be space-padded on the left (right-justified). |
4648|   %-8s | Will print the string as it is. If the string has less than 8 characters, the output will be space-padded on the right (left-justified). |
4749
4850## Example of usage
4951
5052``` pascal
5153procedure TForm1.Button1Click(Sender: TObject);
5254var
5355 title, title2,
5456 underline,
5557 line, row1, row2, row3,
5658 fmt : string;
5759 i : integer;
5860begin
5961 fmt := '%-12s';
6062 title := format(fmt,['Column 1']) + format(fmt,['Column 2']) ;
6163 for i := 1 to 12 do underline := underline + '-';
6264 underline := underline + underline ;
6365 fmt := '%-12d';
6466 line := format(fmt,[15]) + format(fmt,[8]) ;
6567 fmt := '%12s';
6668 title2 := format(fmt,['Column 1']) + format(fmt,['Column 2']) ;
6769 fmt := '%12d';
6870 row1 := format(fmt,[15]) + format(fmt,[8]) ;
6971 row2 := format(fmt,[1005]) + format(fmt,[809]) ;
7072 fmt := '%12.5d';
7173 row3 := format(fmt,[1005]) + format(fmt,[809]) ;
7274 Memo1.Lines.Add( title );
7375 Memo1.Lines.Add( underline );
7476 Memo1.Lines.Add( line );
7577 Memo1.Lines.Add( '' );
7678 Memo1.Lines.Add( title2 );
7779 Memo1.Lines.Add( underline );
7880 Memo1.Lines.Add( row1 );
7981 Memo1.Lines.Add( row2 );
8082 Memo1.Lines.Add( row3 );
8183end;
8284```
8385
8486The above statements will create a set of strings that look like:
8587
86-`Column 1    Column 2    `\
87-`------------------------`\
88-`15          8           `
88+```text
89+Column 1 Column 2
90+------------------------
91+15 8
92+```
8993
90-`    Column 1    Column 2`\
91-`------------------------`\
92-`          15           8`\
93-`        1005         809`\
94-`       01005       00809`
94+```text
95+ Column 1 Column 2
96+------------------------
97+ 15 8
98+ 1005 809
99+ 01005 00809
100+```
95101
96102## See also
97103
98104- [[doc:rtl/sysutils/format.html|Format]]
99105- [[doc:rtl/sysutils/floattostrf.html|FloatToStrF]]
100106- [[doc:rtl/sysutils/formatfloat.html|FormatFloat]]
101107- [[doc:rtl/sysutils/inttohex.html|IntToHex]] - Convert an integer into a [[Hexadecimal|hexadecimal]] string.
102108
103109---
104110*From the Free Pascal wiki, page "Format function", as it stood on 2022-08-06 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](/wiki-contact).*

Back to history