No edit summary |
m (→References) Tag: WoW API docs |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{luaapi}} |
{{luaapi}} |
||
+ | Formats a string by substituting arguments. |
||
− | string.format( |
+ | result = string.format(pattern, ...) |
− | format(formatstring, e1, e2, ...) |
||
+ | result = pattern:format(...) |
||
+ | result = format(pattern, ...) |
||
+ | == Arguments == |
||
− | Create a formatted string from the format and arguments provided. This is similar to the printf("format",...) function in C. An additional option %q returns string in a format that can safely be read back by Lua interpreter (puts quotes around a string and escapes special characters), but used by World of Warcraft to preparse all strings before saving them between sessions. |
||
+ | ;pattern : <span class="apitype">string</span> - Contains directives like <code>%s</code> or <code>%d</code> (see details). Also called the ''format string''.<ref name="pil20">{{ref web|url=https://www.lua.org/pil/20.html|date=2004|chapter=20|title=Programming in Lua|author=Roberto lerusalimschy}}</ref> |
||
+ | ;... : ''Variable arguments'' - Substituted into each directive; the type must be a kind that can convert to the type specified by the directive. |
||
+ | == Returns == |
||
− | * c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as an argument. |
||
+ | ;result : <span class="apitype">string</span> |
||
− | * q and s expect a string. |
||
+ | == Details == |
||
+ | {| class="darktable zebra" |
||
+ | ! Base Directives !! Description |
||
+ | |- |
||
+ | | <code>%c</code> || [[Valid chat message characters|Character]] |
||
+ | |- |
||
+ | | <code>%d</code>, <code>%i</code> || Integer |
||
+ | |- |
||
+ | | <code>%e</code>, <code>%E</code> || Scientific notation |
||
+ | |- |
||
+ | | <code>%f</code> || Floating number |
||
+ | |- |
||
+ | | <code>%g</code>, <code>%G</code> || Floating number or scientific notation |
||
+ | |- |
||
+ | | <code>%o</code> || Octal |
||
+ | |- |
||
+ | | <code>%x</code>, <code>%X</code> || Hexadecimal |
||
+ | |- |
||
+ | | <code>%s</code> || String |
||
+ | |- |
||
+ | | <code>%q</code> || Escaped string with quotes |
||
+ | |- |
||
+ | | <code>%u</code> || Unsigned |
||
+ | |- |
||
+ | | <code>%%</code> || Escaped percent sign |
||
+ | |} |
||
+ | |||
+ | {| class="darktable zebra" |
||
+ | ! Modifiers !! Examples !! Description |
||
+ | |- |
||
+ | | Number || <code>%5d</code>, <code>%3s</code> || Minimum digits or string length with leading spaces |
||
+ | |- |
||
+ | | Zero and number || <code>%05d</code>, <code>%03s</code> || Minimum digits or string length with leading zeroes |
||
+ | |- |
||
+ | | Decimal and number || <code>%.2f</code>, <code>%.4s</code> || Decimal places, significant digits or maximum string length |
||
+ | |- |
||
+ | | Plus sign || <code>%+d</code>, <code>#+f</code> || Leading sign to align with negative values. |
||
+ | |- |
||
+ | | Space || <code>% d</code>, <code># f</code> || Leading space to align with negative values. |
||
+ | |- |
||
+ | | Hash || <code>%#o</code>, <code>%#x</code> || Include octal/hexadecimal prefixes. |
||
+ | |- |
||
+ | | Number and dollar sign || <code>%2$d</code>, <code>%1$s</code> || Selects arguments out of order. |
||
+ | |} |
||
+ | |||
+ | |||
+ | == Examples == |
||
> = string.format("%s %q", "Hello", "Lua user!") -- string and quoted string |
> = string.format("%s %q", "Hello", "Lua user!") -- string and quoted string |
||
Hello "Lua user!" |
Hello "Lua user!" |
||
+ | |||
> = string.format("%c%c%c", 76,117,97) -- char |
> = string.format("%c%c%c", 76,117,97) -- char |
||
Lua |
Lua |
||
+ | |||
> = string.format("%e, %E", math.pi,math.pi) -- exponent |
> = string.format("%e, %E", math.pi,math.pi) -- exponent |
||
3.141593e+000, 3.141593E+000 |
3.141593e+000, 3.141593E+000 |
||
+ | |||
> = string.format("%f, %g, %.2f", math.pi, math.pi, math.pi) -- float and compact float |
> = string.format("%f, %g, %.2f", math.pi, math.pi, math.pi) -- float and compact float |
||
3.141593, 3.14159, 3.14 |
3.141593, 3.14159, 3.14 |
||
+ | |||
> = string.format("%d, %i, %u", -100,-100,-100) -- signed, signed, unsigned integer |
> = string.format("%d, %i, %u", -100,-100,-100) -- signed, signed, unsigned integer |
||
-100, -100, 4294967196 |
-100, -100, 4294967196 |
||
+ | |||
> = string.format("%o, %x, %X", -100,-100,-100) -- octal, hex, hex |
> = string.format("%o, %x, %X", -100,-100,-100) -- octal, hex, hex |
||
37777777634, ffffff9c, FFFFFF9C |
37777777634, ffffff9c, FFFFFF9C |
||
+ | > = string.format("%2$d -> %1$03d", 1, 34) -- 2nd arg, 1st arg with leading zeroes |
||
− | <br> |
||
+ | 34 -> 001 |
||
− | |||
+ | |||
− | Conversions can be applied to the n-th argument in the argument list, rather than the next unused argument. In this case, the conversion character % is replaced by the sequence %d$, where d is a decimal digit in the range [1,9], giving the position of the argument in the argument list. For instance, the call format("%2$d -> %1$03d", 1, 34) will result in "34 -> 001". The same argument can be used in more than one conversion. |
||
⚫ | |||
− | |||
− | <br> |
||
− | |||
− | Note: if you want a % character to be output as part of your format string, you need to enter %%. ie. |
||
⚫ | |||
Ratio is 12 % |
Ratio is 12 % |
||
⚫ | |||
− | <br> |
||
⚫ | |||
This macro prints out the time remaining on the Polymorph debuff in seconds, rounded to the nearest whole number: |
This macro prints out the time remaining on the Polymorph debuff in seconds, rounded to the nearest whole number: |
||
<pre> |
<pre> |
||
Line 40: | Line 91: | ||
== External links == |
== External links == |
||
* http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted-Output |
* http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted-Output |
||
+ | |||
+ | == References == |
||
+ | {{Reflist}} |
Revision as of 02:28, 26 December 2021
Formats a string by substituting arguments.
result = string.format(pattern, ...) result = pattern:format(...) result = format(pattern, ...)
Arguments
- pattern
- string - Contains directives like
%s
or%d
(see details). Also called the format string.[1] - ...
- Variable arguments - Substituted into each directive; the type must be a kind that can convert to the type specified by the directive.
Returns
- result
- string
Details
Base Directives | Description |
---|---|
%c |
Character |
%d , %i |
Integer |
%e , %E |
Scientific notation |
%f |
Floating number |
%g , %G |
Floating number or scientific notation |
%o |
Octal |
%x , %X |
Hexadecimal |
%s |
String |
%q |
Escaped string with quotes |
%u |
Unsigned |
%% |
Escaped percent sign |
Modifiers | Examples | Description |
---|---|---|
Number | %5d , %3s |
Minimum digits or string length with leading spaces |
Zero and number | %05d , %03s |
Minimum digits or string length with leading zeroes |
Decimal and number | %.2f , %.4s |
Decimal places, significant digits or maximum string length |
Plus sign | %+d , #+f |
Leading sign to align with negative values. |
Space | % d , # f |
Leading space to align with negative values. |
Hash | %#o , %#x |
Include octal/hexadecimal prefixes. |
Number and dollar sign | %2$d , %1$s |
Selects arguments out of order. |
Examples
> = string.format("%s %q", "Hello", "Lua user!") -- string and quoted string Hello "Lua user!" > = string.format("%c%c%c", 76,117,97) -- char Lua > = string.format("%e, %E", math.pi,math.pi) -- exponent 3.141593e+000, 3.141593E+000 > = string.format("%f, %g, %.2f", math.pi, math.pi, math.pi) -- float and compact float 3.141593, 3.14159, 3.14 > = string.format("%d, %i, %u", -100,-100,-100) -- signed, signed, unsigned integer -100, -100, 4294967196 > = string.format("%o, %x, %X", -100,-100,-100) -- octal, hex, hex 37777777634, ffffff9c, FFFFFF9C > = string.format("%2$d -> %1$03d", 1, 34) -- 2nd arg, 1st arg with leading zeroes 34 -> 001 > = string.format("Ratio is %u %%",12) -- unsigned, escaped % Ratio is 12 %
Macro Example
This macro prints out the time remaining on the Polymorph debuff in seconds, rounded to the nearest whole number:
/run for i=1,40 do local n,_,_,_,_,_,x,_,_=UnitDebuff("focus",i);if (n=="Polymorph")or (n=="Polymorph(Pig)")or (n=="Polymorph(Turtle)")then SendChatMessage('''format("%.0f"''',-1*(GetTime()-x)).." secs left on "..UnitName("focus").."'s CC!","EMOTE");end end
External links
References
- ^ Roberto lerusalimschy 2004-04-19. Programming in Lua.