String Formatting

From Legacy Roblox Wiki
Jump to navigationJump to search
The order for string formatting is
%[flags][width][.precision]specifier


string.format(formatstring, ...) String formatting is used, well, to format strings. This is done through the string.format function; something that requires specific instructions. These instructions follow the format %[flags][width][.precision]specifier. string.format accepts unlimited arguments.

Specifiers

The most important part of string formatting is the specifiers:

Specifier Accepts Outputs Example
c integer corresponding ASCII character 3
d or i integer decimal representation 321
e/E float scientific notation using lowercase e/E 3.296e2/3.296E2
f float 3231.1231
g/G float The shorter of e/E and f 3E14/3e14
o integer octal representation 610
q string Formats a string in a form suitable to be safely read back by the Lua interpreter: the string is written between double quotes, and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly escaped when written. "print(\"Hi\")"
s string Hello, World!
u positive integer decimal representation 3131
x/X integer Hexadecimal representation 7fa/7FA
% A % followed by another % will return the % sign itself %

Examples

print( ("The magic word is %s"):format("roblox") )
print( ("The magic word is %q"):format("roblox") )

The magic word is roblox

The magic word is "roblox"
print( ("The scientific notation of %d is %E"):format(3901, 3901) )
The scientific notation of 3901 is 3.901000E+03
print( ("It is shorter to use scientific notation for %G"):format(3000000) )
It is shorter to use scientific notation for 3E+06
print( ("%q"):format('Ahaha "quotes", a \n new line, and \0 an embedded zero!') )

"Ahaha \"quotes\", a \

new line, and \0 an embedded zero!"
print( ("Look! A string of hexadecimal characters --> %x"):format(5002) )
Look! A string of hexadecimal characters --> 3a9a

Flags and Width

Flag Description
- Left-justify the given field width. Right justification is the default (see width below)
+ Forces to precede by a + or - sign, even if the number is positive. The default only shows the - sign in front of a negative number.
(space) A blank space is inserted before the value
# Used with o and x/X, writes a 0 (octal) or 0x/0X (hex) before the values for values other than zero.
Used with e/E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
0 Left-pads the number with 0s instead of empty spaces (see width below)
width Description
(number) Minimum number of characters to return. If the number of characters to be formatted is less than this number, then the result is padded with blank spaces. Otherwise, nothing happens.
Example
print(string.format("%-10d", 300))

Output:
300       -- There are 7 spaces there. I counted :D


print(string.format("%+i is less than %i", 300, 3000)) -- First %i for the second argument, second %i for the third

Output:
+300 is less than 3000


print(string.format("You can declare a hexadecimal number by starting it with 0x, like this: %#x.", 1))

Output:
You can declare a hexadecimal number by starting it with 0x, like this: 0x1.


print(string.format("There is a% i%% chance of rain in Seattle, today.", 300))

Output:
There is a 300% chance of rain in Seattle, today.


print(string.format("%012i", 300))

Output:
000000000300


Precision

.precision Description
.number For integer specifiers (d, i, o, u, x/X): precision specifies the minimum number of digits to be returned. If the value to be formatted is shorter than this number, the result is padded with leading zeros. A precision of 0 means that no character is written for the value 0.
For e/E and f specifiers: this is the number of digits to be printed after the decimal point.
For g/G specifiers: This is the maximum number of digits (before the e/E, if present).
For s: this is the maximum number of characters to be returned.
For c and q types: it has no effect.

When no precision is specified, the default is 1. If the period is given without a value for precision, 0 is assumed.

Example
print(string.format("%012.3E", 301141)) 
-- Pad it with 0s to make sure there are 12 digits, put 3 numbers before the E, and return it in scientific notation

Output:
0003.011E+05


print(string.format("string.format > string.sub. %.3s", "Harry Potter trolls!")) -- return first 3 letters of the string

Output:
string.format > string.sub. Har


print(string.format("Once upon a time, there was a magic pony named %s, it had %i horns, and it's location in RAM was %#.9x.", "dap300", 0, 10000000000))

Output:
Once upon a time, there was a magic pony named dap300, it had 0 horns, and it's location in RAM was 0x2540be400.


See Also

http://www.cplusplus.com/reference/clibrary/cstdio/printf/