String.Format

Syntax

Format(FormatString: String; Args: Array): String;

Parameters

FormatString. Element view format.

Args. Array of elements that must be viewed in the specified format.

Description

The Format method returns the string formatted according to the specified instructions.

Comments

The method is used to format various numbers and date or time. Different formatting parameters can be used for different data types. All formatting parameters are specified in the FormatString parameter. Values that must be formatted in the Args parameter. Each formatted value in the FormatString parameter is specified as follows: {x[,y:Az]}.

{x[,y:Az]}
x The index of the Args array, to which formatting should be applied.
y Alignment specifier. It is used to format integers and real numbers. It determines the total length of the formatted string, including the fractional part and the point.
A Formatting parameters.
z Precision specifier. It is used to format integers and real numbers. It determines the number of digits in the fractional part.

Elements of the y, A and z format are optional, if they are not specified, unformatted value is returned.

Number Formatting Parameters

Formatting parameter Brief description
{0:G} General format. It is used to display expression with fixed precision. A number is converted into a shorter record from a record with fixed point or from an exponential record depending on the number type and availability of precision specifier. When no precision specifier is set, nine decimal positions are displayed.
{0:F} Displaying a value with fixed precision. By default, values are displayed with precision to two digits.
{0:N} Standard numeric formatting, using separators. A space is used as a separator.
{0:P} Percentage number format.

Formatting Parameters for Date and Time

Formatting parameter Brief description
{0:d} Short date format.
{0:D} Full date format.
{0:t} Short time format.
{0:T} Full time format.
{0:f} Full date format and short time format.
{0:F} Full date format and full time format.
{0:g} Short date and time format.
{0:G} Short date format and full time format.

Example

Sub UserProc;
Var
    Arr: Array Of Variant;
    s: String;
Begin
    Arr := New Variant[5];
    Arr[0] := Integer.MaxValue;
    Arr[1] := 123456.123456;
    Arr[2] := Math.Pi;
    Arr[3] := Decimal.MaxValue;
    Arr[4] := 0.5;
    s := String.Format("Numbers with different formatting:" + #13 + #10 +
        "'{0:G1}' - with precision restriction 1 character; " + #13 + #10 +
        "'{1:F}' - with fixed precision 2 character; " + #13 + #10 +
        "'{2,8:F4}' - with fixed precision and length restriction; " + #13 + #10 +
        "'{3:N}' - formatting with delimiter; " + #13 + #10 +
        "'{4:P}' - Percentage format." , Arr);
    Debug.WriteLine(s);
End Sub UserProc;

After executing the example the development environment console displays the following string:

Numbers with different formatting.

'2147483647,0' - with precision restriction 1 character.

'123456,12' - with fixed precision 2 characters.

'  3,1416' - with fixed precision and length restriction.

'79 228 162 514 264 337 593 543 950 336,00' - formatting with delimiter.

'50,00%' - percentage format.

Sub UserProc;
Var
    d: DateTime;
    s, s1, s2, s3: String;
Begin
    d := DateTime.Compose(200611101230301);
    s := String.Format("{0:d}", d);
    s1 := String.Format("Today {0:D}", d);
    s2 := String.Format("Time {0:t}", d);
    s3 := String.Format("{0:T}", d);
End Sub UserProc;

After executing the example the variables s, s1, s2 ,s3 will contain the following values:

Variable Value
s '10.11.2006'
s1 'Today 10 November 2006'
s2 'Time 12:30'
s3 '12:30:30'

See also:

String