Codectionary / Developer documentation / Lua

Lua formatted text

string.format creates display text with conversion specifiers and widths. Formatting a number for display does not change its stored value.

Syntax

string.format("%s: %03d", name, count)

Examples

Lua formatted text

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

print(string.format("%s: %03d", "Tasks", 7))
local price = 12.5
print(string.format("Price %.2f", price))
  1. Output: Tasks: 007 and Price 12.50 in the default C locale. %03d pads an integer; %.2f displays two fractional digits.

Best practices

  • Keep format strings under application control and match argument types. Formatting is not exact monetary arithmetic.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading