Format Function

Purpose

Returns a String containing an expression formatted according to instructions contained in a format expression.

Syntax

$ = Format(expression[, formsexp])

expression: string, numerical, variant or date
formsexp : string

Description

Format() is a conversion function for which you can format numeric, string, variant and date expressions.

Generally, the output of numeric, variant and date expressions can be altered using Mode Format, Mode Lang but not with Mode Date; however,all the predefined formats use your system's Locale settings rather than GFA's internal register and these can only be altered using Mode Lang (where this is the case, it is noted in the text). If you are dealing with dates - and sometimes numeric expressions - which will be displayed in different locales it is generally better to use the pre-defined formats if possible as the return values are automatically created using Windows Locale APIs.

The Format function can be used with or without the second parameter; if used without, then it is assumed to be an empty or null string and the function returns an expression accordingly. For example:

Debug Format(2)         // Returns 2 without a leading character

Debug Format(Date)      // Returns the current date in Short format

Debug Format(Now)       // Returns the current date in Short Format and time in Long format

Debug Format(Now, "")   // Same as above

°Debug Format("GFA")    // This will fail in the IDE as GFA Strings are not supported...

Local a = "GFA"

Debug Format(a)         //  ...but Strings in a Variant are supported and this returns GFA

The power of the function becomes apparent when the formsexp parameter is used: this parameter can be constructed to adapt the expression to the form you require. For example:

Debug Format(Now, "Long Date")                  // Returns the current date in Long Date format (e.g. 21 November 2022)

Debug Format(3.41, #34"£"#34 & "###,##0.00##"// Returns £3.41

In addition, the formsexp can be divided into sections, each separated using a semi-colon (;); the number of sections available is dependant on the variable type passed as expression as follows:

All but the first section can be omitted and, if so, any values which correspond to the missed section will be formatted according to the first one instead, as below:

Debug Format(-2.22, #34"Positive"#34";"#34"Negative"#34";"#34"Zero"#34) // Returns "Negative"

Debug Format(-2.22, #34"Positive"#34";;"#34"Zero"#34)                   // Returns "Positive"

Finally, if only one section is used, the semi-colons are not required and can be omitted.

If a value is passed which is incompatible with the format type to be output, zero is generally assumed if the value is not convertible; the below show how GB32 handles incorrect values that are passed:

Trace Format("Today", "dd/mm/yyyy")     // Converts to zero and returns 30/12/1899 (or 0)

Trace Format(2.44, "@@@@")              // Converts to text with leading space and returns ' 2.4' (without apostrophes)

Trace Format(#12/12/2023#, "@@@@")      // Converts to text in short date format and returns 12/1 (no leading space)

Trace Format("Today", "#,##0.00")       // Converts to number and returns 0.00

Trace Format("2.33", "#,##0.00")        // Converts to number and returns 2.33

This list is not exhaustive but gives an idea of what to expect.

General Formatting Show

String Formatting Show

User-defined Number (& Variant) Formatting Show

Predefined Number (& Variant) Formatting Show

User-defined Date and Time Formatting Show

Predefined Date/Time Formatting Show

The Format function is quite versatile and the value passed can be split into sections or used repeatedly to produce different values, as in the following examples:

Debug Format(#10/15/2023#, "dddddd 'is a' dddd 'in week 'ww' of the year.'") // Prints "15 October 2023 is a Sunday in week 41 of the year."

Debug Format(12306344, "#0' million, '000' thousand, '0' hundred and '00'.'") // Prints "12 million, 306 thousand, 3 hundred and 44."

Debug Format("GFABasic", "'The first three letters of the word are '&&&' and the rest are '&&&&&'.'") // Print "The first three letters of the word are GFA and the rest are Basic."

Example

Use the following example to see how the formatting appears on your system:

Debug Format(12.345, "$#,##0.00")

Debug Format(44576, "dd/mm/yyyy hh:nn:ss")

Debug Format(12.345, "General Number")

Debug Format(12.345, "Currency")

Debug Format(12.345, "Fixed")

Debug Format(12.345, "Standard")

Debug Format(12.345, "Percent")

Debug Format(12.345, "Scientific")

Debug Format(12.345, "General Date")

Debug Format(12.345, "Long Date")

Debug Format(12.345, "Medium Date")

Debug Format(12.345, "Short Date")

Debug Format(12.345, "Long Time")

Debug Format(12.345, "Medium Time")

Debug Format(12.345, "Short Time")

Remarks

See Also

Using

{Created by Sjouke Hamstra; Last updated: 18/10/2023 by James Gaite; Other Contributors: Jean-Marie Melanson}