Built-in routines

sprintf & fmt — formatting

Full

printf-style formatting of numbers and strings with %-directives.

sprintf builds a string from a format template and arguments, using the familiar %-directives. The .fmt method is the same formatting applied to a single value.

Numbers, precision, padding #

%d is an integer, %.2f a fixed-point float, %05d zero-pads to width 5.

say sprintf("%d apples", 5);
say sprintf("%.2f", pi);
say sprintf("%05d", 42);
Output
5 apples
3.14
00042

Radix directives #

%x, %o, %b render an integer in hex, octal, and binary.

say sprintf("%x", 255);
say sprintf("%b", 10);
say sprintf("%o", 64);
Output
ff
1010
100

String alignment #

%-6s left-justifies in a field of width 6; %6s right-justifies.

say sprintf("%-6s|", "hi");
say sprintf("%6s|", "hi");
Output
hi    |
    hi|

The .fmt method #

.fmt formats a single value with one directive — handy in a map or interpolation.

say 255.fmt("%04x");
say 42.fmt("%+d");
Output
00ff
+42

Notes #