String Formatting

How to use string formatting, and what it is.

by Halalaluyafail3

Author Avatar

In Lua there is a function for formatting strings.

string.format(string str,...)

Returns a string formatted according to str using values ...

Syntax

The syntax for a pattern: %[flags][width][.precision]specifier

A % followed by another % represents the % itself.

Everything else represents itself, so

print(string.format("%% Hello %s","world"))

outputs '% Hello world'

Specifiers

a - Hexadecimal float representation (e.g 0x1.f4p+6) Accepts numbers A - Uppercase hexadecimal float representation (e.g 0X1.F4P+6) Accepts numbers c - Character represenation for given value (e.g A, for value 65) Accepts integers d - Decimal representation (e.g 52) Accepts integers e - Scientific notation (e.g 5.2e+01) Accepts numbers E - Uppercase scientific notation (e.g 5.2E+01) Accepts numbers f - Decimal float representation (e.g 5.2) Accepts numbers g - Shorter scientific notation (e.g 5e+01) Accepts numbers G - Uppercase shorter scientific notation (e.g 5E+01) Accepts numbers i - Equivalent to d Accepts integers o - Octal representation, unsigned (e.g 177) Accepts integers q - Safely represent a string which can be read back by the interpreter (e.g "print"Hello World"") Accepts strings s - Inserts the string (e.g hi) u - Decimal representation, with unsigned numbers (e.g 52) Accepts unsigned integers (negative integers overflow) x - Hexadecimal representation, unsigned (e.g 7f) Accepts integers X - Uppercase hexadecimal representation, unsigned (e.g 7F) Accepts integers

In 5.1, a and A do not exist. Flags, width, and length are not guaranteed to work with the a and A specifiers.

Flags

'-' - Left justify with width. (see Width) When using both the hyphen flag and the zero flag, the zero flag will have no effect. '+' - Forces a plus to precede a number. Has no effect on negative numbers, or things other than numbers. Using the plus flag and the space flag disables the space flag. Doesn't work with specifiers o,u,x, and X. ' ' - Forces a space to precede a number. Has no effect on negative numbers, or things other than numbers. Doesn't work with specifiers o,u,x, and X. '0' - Pads with zeros instead of spaces. (see Width) Doesn't work with specifiers c, s and q. '#' - The octothorpe flag works with specific specifiers, listed below. o, precedes the number with a zero x, precedes the number with 0x X, precedes the number with 0X a, forces the result to contain a decimal part A, forces the result to contain a decimal part e, forces the result to contain a decimal part E, forces the result to contain a decimal part f, forces the result to contain a decimal part g, forces the result to contain a decimal part G, forces the result to contain a decimal part For all other specifiers, the octothorpe flag has no effect. For e,E, and f the octothorpe flag does little as they contain the decimal part by default. But this does still have an effect when the precision (see Precision) is 0, forcing the number to have a period. (this trick works with a,A,g and G too)

Width

Minimum length of the result. Any extra characters needed are added based on the specifier and flags. The q specifier does not work with width. By default Width adds characters to the left, but this can be changed with the hyphen flag. Extra characters are spaces, unless specified to be 0 with the zero flag.

Precision

Behavior dependant upon specifier. For specifiers d,i,o,u,x, and X, the precision specifies the minimum number of digits. If the value to be formatted is shorter than this number, leading zeros are added. A precision of 0 means that no character is written for the value 0. Using precision with one of these specifiers makes the zero flag have no effect. For specifiers a,A,e,E, and f, the precision is the number of digits after the decimal point. For specifier g, this is the maximum number of digits present before the e, if present. For specifier G, this is the maximum number of digits present before the E, if present. For specifier s, this is the maximum number of characters to be used. For specifiers q and c, this has no effect.

Examples

%f %d %.2e %5s %#X %+8.5i %#+a % -+#099.99f

View in-game to comment, award, and more!