Difference between revisions of "Format function"
From Free Pascal wiki
Jump to navigationJump to searchm (Add url) |
|||
Line 1: | Line 1: | ||
− | + | The '''format''' function formats a series of values into a string. | |
− | + | For an in-depth explanation see the [http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/format.html on line documentation]. | |
− | |||
− | The format function formats a series of values into a string. For an in-depth explanation see the [http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/format.html on line documentation]. | ||
The formatting rule for each given argument is: | The formatting rule for each given argument is: | ||
% [ArgumentIndex] ['-'] [width] [. precision] ArgumentType | % [ArgumentIndex] ['-'] [width] [. precision] ArgumentType | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | | d || Decimal (integer) | ||
+ | |- | ||
+ | | e || Scientific | ||
+ | |- | ||
+ | | f || Fixed | ||
+ | |- | ||
+ | | g || General | ||
+ | |- | ||
+ | | m || Money | ||
+ | |- | ||
+ | | n || Number (floating) | ||
+ | |- | ||
+ | | p || Pointer | ||
+ | |- | ||
+ | | s || String | ||
+ | |- | ||
+ | | u || Unsigned decimal | ||
+ | |- | ||
+ | | x || Hexadecimal | ||
+ | |} | ||
− | = | + | === Integer formatting === |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | == Integer formatting == | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
Line 34: | Line 42: | ||
| %.8d | | %.8d | ||
| Will pint the integer as it is. If the number of digits is less than 8, the output will be padded on the left with zeroes. | | Will pint the integer as it is. If the number of digits is less than 8, the output will be padded on the left with zeroes. | ||
− | |||
|} | |} | ||
− | == String formatting == | + | === String formatting === |
− | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
Line 53: | Line 59: | ||
== Example to use == | == Example to use == | ||
− | |||
<syntaxhighlight> | <syntaxhighlight> | ||
− | |||
− | |||
procedure TForm1.Button1Click(Sender: TObject); | procedure TForm1.Button1Click(Sender: TObject); | ||
var | var | ||
Line 88: | Line 91: | ||
Memo1.Lines.Add( row3 ); | Memo1.Lines.Add( row3 ); | ||
end; | end; | ||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
The above statements will create a set of strings that look like: | The above statements will create a set of strings that look like: | ||
Line 105: | Line 104: | ||
1005 809 | 1005 809 | ||
01005 00809 | 01005 00809 | ||
− | |||
− | |||
− | |||
== See also == | == See also == | ||
− | |||
* [[doc:rtl/sysutils/format.html| Format ]] | * [[doc:rtl/sysutils/format.html| Format ]] | ||
* [[doc:rtl/sysutils/floattostrf.html| FloatToStrF ]] | * [[doc:rtl/sysutils/floattostrf.html| FloatToStrF ]] |
Revision as of 21:20, 26 August 2016
The format function formats a series of values into a string. For an in-depth explanation see the on line documentation.
The formatting rule for each given argument is:
% [ArgumentIndex] ['-'] [width] [. precision] ArgumentType
d | Decimal (integer) |
e | Scientific |
f | Fixed |
g | General |
m | Money |
n | Number (floating) |
p | Pointer |
s | String |
u | Unsigned decimal |
x | Hexadecimal |
Integer formatting
%d | Will print the integer as it is. |
%8d | Will pint the integer as it is. If the number of digits is less than 8, the output will be padded on the left. |
%-8d | Will pint the integer as it is. If the number of digits is less than 8, the output will be padded on the right. |
%.8d | Will pint the integer as it is. If the number of digits is less than 8, the output will be padded on the left with zeroes. |
String formatting
%s | Will print the string as it is. |
%8s | Will pint the string as it is. If the string has less than 8 characters, the output will be padded on the left. |
%-8s | Will pint the string as it is. If the string has less than 8 characters, the output will be padded on the left. |
Example to use
procedure TForm1.Button1Click(Sender: TObject);
var
title, title2,
underline,
line, row1, row2, row3,
fmt : string;
i : integer;
begin
fmt := '%-12s';
title := format(fmt,['Column 1']) + format(fmt,['Column 2']) ;
for i := 1 to 12 do underline := underline + '-';
underline := underline + underline ;
fmt := '%-12d';
line := format(fmt,[15]) + format(fmt,[8]) ;
fmt := '%12s';
title2 := format(fmt,['Column 1']) + format(fmt,['Column 2']) ;
fmt := '%12d';
row1 := format(fmt,[15]) + format(fmt,[8]) ;
row2 := format(fmt,[1005]) + format(fmt,[809]) ;
fmt := '%12.5d';
row3 := format(fmt,[1005]) + format(fmt,[809]) ;
Memo1.Lines.Add( title );
Memo1.Lines.Add( underline );
Memo1.Lines.Add( line );
Memo1.Lines.Add( '' );
Memo1.Lines.Add( title2 );
Memo1.Lines.Add( underline );
Memo1.Lines.Add( row1 );
Memo1.Lines.Add( row2 );
Memo1.Lines.Add( row3 );
end;
The above statements will create a set of strings that look like:
Column 1 Column 2 ------------------------ 15 8
Column 1 Column 2 ------------------------ 15 8 1005 809 01005 00809
See also
- Format
- FloatToStrF
- FormatFloat
- IntToHex - Convert an integer into a hexadecimal string.