FORMAT
The FORMAT function formats integer values into a string.
The format strings used by FORMAT may also be used in XJRunner when formatting dates and times in the filenames and headers of log files.
Syntax
FORMAT( INT value, STRING formatString )
Parameters
- value
- The value to be formatted. This may be an integer or it may be a date and time representation obtained via NOW, allowing dates and times to be displayed.
- formatString
- A string that specifies how the value is to be formatted (see Format Strings below for more details).
Return value
A STRING containing the formatted value.Format Strings
The formatString is a pattern that specifies how the integer is to be formatted. A format starts with a '%' and then any characters up to the first alphabetic character. Any other characters in the string are printed verbatim. If more than one format is specified in the string then the integer will be reinterpreted - this is mainly useful for printing date and time strings, and allows you to print more than one date or time component at once.
A literal percent sign may be printed by using two percent signs.
When used to assign formatted integers to string variables the FORMAT function can act as a reciprocal function to STRTOINT
The format is of the form:
% {flags} {width} type
Flags is a combination of '-', '#' and '0'.
Width is a numeric value specifying a minimum width to make the value printed; values may be padded with zeroes or spaces depending on the flags specified. If the formatted value is wider than the width specified it is not truncated.
Type is a character which specifies how to interpret the value:
The following type values interpret the value as a normal integer:
Type | Description |
---|---|
b | Format the integer as a binary string. |
i | Format the integer as a decimal string. |
x | Format the integer as a hexadecimal string using lower-case characters. |
X | Format the integer as a hexadecimal string using upper-case characters. |
The flags have the following effects in these cases:
Flag | Description |
---|---|
- | Left align the field; normally integers are right-aligned. |
# | For '%b', '%x' and '%X' add the prefixes '0b', '0x' and '0X' respectively. |
0 | If the formatted value is not as wide as the specified width, pad it with zeroes rather than spaces. |
If a width is specified along with the '#' flag, any padding characters (either spaces or zeroes) are inserted between the prefix and the number. The width does not include the width of any prefix.
Dates and Times
Dates and times are represented in XJEase as an integer value. Such values are typically obtained from the NOW function. Date and times are always converted to local time before formatting (the underlying storage is in UTC).
The following type values interpret the value as a date/time value:
Type | Description |
---|---|
S | The number of seconds. |
M | The number of minutes. |
h | The number of hours in 12 hour format. |
H | The number of hours in 24 hour format. |
d | The day of the month. |
w | The day of the week in numeric format, where 0 is Sunday and 6 is Saturday. |
a | An abbreviated version of the day of the week. |
A | The day of the week in full. |
m | The month in numeric format, where 1 is January and 12 is December. |
n | An abbreviated version of the month. |
N | The month in full. |
y | A two digit version of the year. |
Y | The year in full. |
The only flag supported for date formats is '#', which will suppress any padding with zeroes for the following formats: 'y', 'Y', 'd', 'h', 'H', 'M' and 'S'.
Finally, the following formats are simple shortcuts to display either the date or the time using the formats defined for the current user's locale. The actual format used will depend on how the user has configured Windows.
Type | Description |
---|---|
{Long Date} | A long version of the date, e.g. "Sunday 1 February 2004" |
{Short Date} | A shorter version of the date, e.g. "01/02/2004" |
{Date} | A synonym for {Long Date}. |
{Time} | The time, e.g. "12:34:00" |
Example
INT x := 42; INT datetime := NOW(); STRING binaryFormat; PRINT("Decimal: ", FORMAT(x, "%i\n")); PRINT("Hex: ", FORMAT(x, "%#02x\n")); binaryFormat := FORMAT(x, "%08b\n"); PRINT("Binary: ", binaryFormat); PRINT("datetime: ", datetime, "\n"); PRINT("Long date: ", FORMAT(datetime, "%{Long Date}\n")); PRINT("Date and time: ", FORMAT(datetime, "%{Short Date} %{Time}\n")); PRINT("Custom: ", FORMAT(datetime, "%a, %#d %N %Y - %H:%M\n"));
The output will be:
Decimal: 42 Hex: 0x2a Binary: 00101010 datetime: 12738695549839 Long date: 03 September 2004 Date and time: 03/09/2004 14:32:29 Custom: Fri, 3 September 2004 - 14:32
See Also
XJTAG v4.1.100