Function Call
string.format(formatstring, ...args)
|
Specifier
Input |
Description |
Example |
g |
Auto† |
384.63 |
G |
Auto (uppercase)† |
386 |
d or i |
Signed decimal |
-392 |
u |
Unsigned decimal |
1483 |
f |
Decimal float |
392.65 |
o |
Unsigned octal |
610 |
x |
Unsigned hex |
7fa |
X |
Unsigned hex (uppercase) |
7FA |
a |
Hex float |
-0xc.90fep-2 |
A |
Hex float (uppercase) |
-0XC.90FEP-2 |
e |
Scientific notation |
3.9265e+2 |
E |
Scientific notation (uppercase) |
3.9265E+2 |
c |
Character |
d |
s |
String |
Hello world |
p |
Pointer |
b8000000 |
% |
Literal "%" |
% |
Specifier determines the string format type.
†Auto choses between the more reasonable of e
or f
.
Q Specifier
The Q specifier does not support flags, width, or precision. It outputs Lua values so that it is valid Lua code.
Booleans are written obviously, true, false, nil
.
Strings are written with double quotes and escape sequences where necessary. |
For example, string.format('%q', 'a string with "quotes" and \n new line')
produces:
"a string with \"quotes\" and \
new line"
|
|
Format Specifier
%[flags][width][.precision]specifier
|
Values are formatted and replaced sequentially with ...args.
Only one input can be entered for each argument.
Note the dot before precision.
Flags
' |
Group number by thousands. |
- |
Left justify, see width. |
0 |
Pads with 0s instead of spaces, see width. |
+ |
Adds a + sign for positive numbers. |
(space) |
Inserts a space if there is no sign. |
# |
For o, x, X: Add 0, 0x, 0X to non-zero numbers. For a, A, e, E, f, F, g, G: Always write a decimal point. |
Width
Minimum number of characters to be printed.
If the value is shorter:
The value is padded, affected by - and 0 flags.
If the value is longer:
No truncation happens, note minimum. |
Precision
For d, i, o, u, x, X (integers):
Minimum number of digits, no truncation happens. If the value is shorter, 0-padding happens on the left side. If set to zero, nothing is written for 0.
For a, A, e, E, f, F (floats):
Number of decimal digits to be printed. Defaults to 6.
For g, G:
Maximum number of significant digits.
For s:
Maximum number of characters to be printed. By default all characters are printed. |
Don't forget the dot!
If the period is specified without a value for precision, 0 is assumed.
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by ambigious