string.format
- Lua function string.format Summary Formats a string Prototype `s = string.format (fstr, v1, v2, v3, ...)` Description `Formats the supplied values (v1, v2 etc.) using format string 'fstr', similar to the C function printf. It is an error to supply too few variables for the format string. The format string comprises literal text, and directives starting with %. Each directive controls the format of the next argument. Directives can include flags, width and precision controls. Directives can be: %c - convert a number to a single character (like string.char)
      string.format ("%c", 65) --> A
- %d and %i - output as an integer number
      string.format ("%i", 123.456) --> 123
- %o - convert to octal
      string.format ("%o", "16") --> 20
- %u - convert to an unsigned number Negative numbers will be converted to 4294967296 (2^32) minus the number.
      string.format ("%u", "1234.566") --> 1234
      string.format ("%u", "-1234")    --> 4294966062
- %x - hex (lower-case)
      string.format ("%x", "86543") --> 1520f
- %X - hex (upper-case)
      string.format ("%X", "86543")--> 1520F
- %e - scientific notation, "e" in lower case:
      string.format ("%e", "15") --> 1.500000e+001
- %E - scientific notation, "E" in upper case:
      string.format ("%E", "15") --> 1.500000E+001
- %f - floating point, default to 6 decimal places:
      string.format ("%f", "15.656e4") --> 156560.000000
- %g - Signed value printed in %f or %e format, whichever is more compact for the given value.
      string.format ("%g", "15.656") --> 15.656
- %G - Same as %g except that an upper-case E is used where appropriate.
      string.format ("%G", "15.656e42") --> 1.5656E+043
%q - formats a string in such a way Lua can read it back in. Basically this means it puts a backslash in front of the quote character, backslash itself, newline, and the nul character. The string itself is surrounded by quotes. %s - output a string %% - output a single % character You can optionally supply 'flags width.precision' arguments before the letter. Flags can be: - : left align result inside field + : always prefix with a sign, using + if field positive 0 : left-fill with zeroes rather than spaces (space) : If positive, put a space where the + would have been # : For octal conversion (o), prefixes the number with 0 For hex conversion (x), prefixes the number with 0x For hex conversion (X), prefixes the number with 0X For e, E and f formats, always show the decimal point. For g and G format, always show the decimal point, and do not truncate trailing zeroes Width is the width of the returned field. If the converted number/string is wider than the width it is not truncated. You cannot use "*" as the width (as you can for printf). If you want variable-size strings you can simulate that by modifying the format string on-the-fly. eg. instead of %*g, use "%" .. width .. "g" Precision is the number of decimal places to show for floating-point numbers.
    string.format ("%15.1f", "15.656")   --> '           15.7'
    string.format ("%15.8f", "15.656")   --> '    15.65600000'
    string.format ("%-15.1f", "15.656")  --> '15.7           '
    string.format ("%015.1f", "15.656")  --> '0000000000015.7'
    string.format ("%+015.1f", "15.656") --> '+000000000015.7
    string.format ("%5s", "hi")          --> '   hi'
    string.format ("%5-s", "hi")         --> 'hi   '
` See Also ... Topics Lua base functions Lua bc (big number) functions Lua bit manipulation functions Lua coroutine functions Lua debug functions Lua io functions Lua math functions Lua os functions Lua package functions Lua PCRE regular expression functions Lua script extensions Lua string functions Lua table functions Lua utilities Regular Expressions Scripting Lua functions string.byte (Converts a character into its ASCII (decimal) equivalent) string.char (Converts ASCII codes into their equivalent characters) string.dump (Converts a function into binary) string.find (Searches a string for a pattern) string.gfind (Iterate over a string (obsolete in Lua 5.1)) string.gmatch (Iterate over a string) string.gsub (Substitute strings inside another string) string.len (Return the length of a string) string.lower (Converts a string to lower-case) string.match (Searches a string for a pattern) string.rep (Returns repeated copies of a string) string.reverse (Reverses the order of characters in a string) string.sub (Returns a substring of a string) string.upper (Converts a string to upper-case) (Help topic: lua=string.format) Documentation contents page