Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sprintf

Formats a string according to a printf-style format specification.

Takes a format string followed by a list of arguments (from a pushmark-delimited argument list on the stack) and returns the formatted result string. Supports the standard C-style conversion specifiers:

  • %s string, %d/%i signed decimal, %u unsigned decimal
  • %o octal, %x/%X hex, %b/%B binary
  • %e/%E scientific, %f/%F fixed, %g/%G general float
  • %c character (by codepoint), %p pointer, %% literal percent

Flags: - (left-justify), + (force sign), (space for sign), 0 (zero-pad), # (alternate form). Width and precision may be literal or * (consumed from the argument list). Indexed arguments via %N$ are supported. The %vd vector flag formats a string as dot-separated byte values (e.g. version strings).

Uses a zero-allocation fast path: arguments are peeked from the stack as &[Sv] without cloning, and integer formatting uses stack buffers.

Synopsis

$s = sprintf('%d items at $%.2f each', $count, $price);
$s = sprintf('%05x', $hex_val);
$s = sprintf('%-20s', $name);

See Also

printf, join