Format [v1.1.17+]

Formats a variable number of input values according to a format string.

String := Format(FormatStr [, Values...])

Parameters

FormatStr

A format string composed of literal text and placeholders of the form {Index:Format}.

Index is an integer indicating which input value to use, where 1 is the first value.

Format is an optional format specifier, as described below.

Omit the index to use the next input value in the sequence (even if it has been used earlier in the string). For example, "{2:i} {:i}" formats the second and third input values as decimal integers, separated by a space. If Index is omitted, Format must still be preceded by :. Specify empty braces to use the next input value with default formatting: {}

Use {{} and {}} to include literal braces in the string. Any other invalid placeholders are included in the result as is.

Whitespace inside the braces is not permitted (except as a flag).

Values

Input values to be formatted and inserted into the final string. Each value is a separate parameter. The first value has an index of 1.

To pass an array of values, use a variadic function call:

arr := [13, 240]
MsgBox % Format("{2:x}{1:02x}", arr*)

Format Specifiers

Each format specifier can include the following components, in this order (without the spaces):

Flags Width .Precision ULT Type

Flags which affect output justification and prefixes: - + 0 space #

Width: a decimal integer which controls the minimum width of the formatted value, in characters. By default, values are right-aligned and spaces are used for padding. This can be overridden by using the - (left-align) and 0 (zero prefix) flags.

.Precision: a decimal integer which controls the maximum number of string characters, decimal places, or significant digits to output, depending on the output type. It must be preceded by a decimal point. Specifying a precision may cause the value to be be truncated or rounded.

ULT [v1.1.20+]: specifies a case transformation to apply to a string value -- Upper, Lower or Title. Valid only with the s type. For example {:U} or {:.20Ts}. Lower-case l and t are also supported, but u is reserved for unsigned integers.

Type: a character indicating how the input value should be interpreted. If omitted, it defaults to s.

FlagMeaningDefault
- Left align the result within the given field width (insert spaces to the right if needed). Right align.
+ Use a sign (+ or -) to prefix the output value if it is of a signed type. Sign appears only for negative signed values (-).
0 If width is prefixed by 0, leading zeros are added until the minimum width is reached. If both 0 and - appear, the 0 is ignored. If 0 is specified as an integer format (i, u, x, X, o, d) and a precision specification is also present - for example, {:04.d} - the 0 is ignored. No padding.
space Use a single space to prefix the output value with a space if it is signed and positive. The space is ignored if both the space and + flags appear. No space appears.
#

When it's used with the o, x, or X format, the # flag uses 0, 0x, or 0X, respectively, to prefix any nonzero output value.

When it's used with the e, E, f, a or A format, the # flag forces the output value to contain a decimal point.

When it's used with the g or G format, the # flag forces the output value to contain a decimal point and prevents the truncation of trailing zeros.

Ignored when used with c, d, i, u, or s.

Type CharacterArgumentOutput format
d or i Integer Signed decimal integer.
u Integer Unsigned decimal integer.
x or X Integer Unsigned hexadecimal integer; uses "abcdef" or "ABCDEF" depending on the case of x. The 0x prefix is not included unless the # flag is used, as in {:#x}. For hexadecimal formatting consistent with SetFormat, use 0x{:x} or similar.
o Integer Unsigned octal integer.
f Floating-point Signed value that has the form [ - ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
e Floating-point Signed value that has the form [ - ]d.dddd e [sign]dd[d] where d is one decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, and sign is + or -.
E Floating-point Identical to the e format except that E rather than e introduces the exponent.
g Floating-point Signed values are displayed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than -4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
G Floating-point Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
a Floating-point Signed hexadecimal double-precision floating-point value that has the form [?]0xh.hhhh p±dd, where h.hhhh are the hex digits (using lower case letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point.
A Floating-point Identical to the a format, except that P, rather than p, introduces the exponent.
p Integer Displays the argument as a memory address in hexadecimal digits.
s String Specifies a string. If the input value is numeric, it is automatically converted to a string using the script's current number format before the Width and Precision arguments are applied.
c Character code Specifies a single character by its ordinal value, similar to Chr(n). If the input value is outside the expected range, it wraps around.

Remarks

Unlike printf, size specifiers are not supported. All integers and floating-point input values are 64-bit.

Related

SetFormat, FormatTime

Example

; Simple substitution
s .= Format("{2}, {1}!`r`n", "World", "Hello")
; Padding with spaces
s .= Format("|{:-10}|`r`n|{:10}|`r`n", "Left", "Right")
; Hexadecimal
s .= Format("{1:#x} {2:X} 0x{3:x}`r`n", 3735928559, 195948557, 0)
; Floating-point
s .= Format("{1:0.3f} {1:.10f}", 4*atan(1))

ListVars  ; Use AutoHotkey's main window to display monospaced text.
WinWaitActive ahk_class AutoHotkey
ControlSetText Edit1, %s%
WinWaitClose