Skip to content

@fmt

Import this module with import @fmt.

Formatted output and string formatting functions.

Function Signature Description
printf (format string, args [T]) Print formatted string to stdout
printfln (format string, args [T]) Print formatted string to stdout with trailing newline
eprintf (format string, args [T]) Print formatted string to stderr
eprintfln (format string, args [T]) Print formatted string to stderr with trailing newline
sprintf (format string, args [T]) -> string Return formatted string
sprintfln (format string, args [T]) -> string Return formatted string with trailing newline

One array element per format directive; each is independently i64, u64, f64, string, bool, char, or a bigint (i128/u128/i256/u256, integer directives only). Composite types are rejected.

When args is an array literal, each element is checked against its own directive, so the elements may differ in type ({name, 30}); this is the only place a mixed-type array literal is allowed, and its elements are never unified to a common type. The literal must have exactly as many elements as the format string has directives. When args is an array variable of type [T], every directive must accept T; one element is read per directive, and the program panics if the array has fewer elements than directives. args is required even when the format string has no directives; plain text goes through print or println.

eprintln and eprint are builtins, not fmt module functions; use them without an import.

Format strings use C-style % specifiers:

Specifier Type Description
%d, %i i64 Signed decimal integer
%u u64 Unsigned decimal integer
%f f64 Decimal floating-point
%e f64 Scientific notation
%g f64 Shorter of %f or %e
%s string String
%c char Single character, printed as its UTF-8 encoding
%b bool true / false
%x, %X i64 / u64 Hexadecimal (lowercase / uppercase)
%o i64 / u64 Octal
%% — Literal %

Width, precision, and flags (-, +, space, 0, #) follow standard C printf conventions, limited to the ones each conversion gives a meaning to:

Conversion Flags Precision
%d, %i - + space 0 yes
%u - 0 yes
%x, %X, %o - 0 # yes
%f, %e, %E, %g, %G - + space 0 # yes
%s - yes (maximum bytes)
%c, %b - no

Any other flag or a precision where the table has none is a compile error (E3178). C length modifiers (h, hh, l, ll, L) are not accepted (E3177): each value is formatted at the width of its type, so %d, %i, %u, %x, %X, and %o are automatically widened to their 64-bit form for Grayscale’s i64/u64 types. A width or precision above 2147483647 is a compile error (E3179); C’s printf cannot represent it. Escapes in the format string are decoded before directives are read, so "\x25d" is a %d directive, and a \0 writes a NUL byte rather than ending the format string. %c prints a char (or an integer codepoint) as UTF-8, so 'é' prints as é, and its width counts characters (%3c pads 'é' to three characters, like 'a'); an integer that is not a Unicode scalar value (negative, a surrogate U+D800–U+DFFF, or above U+10FFFF) prints U+FFFD. The same directives also accept i128, u128, i256, and u256, which are rendered from their raw bit pattern (like C printf: %x/%o on a negative value show its two’s-complement form); for a bigint element only width and - apply — the 0, #, +, and space flags and precision are ignored. %f, %c, and %b reject bigints. Composite types (structs, arrays, maps) are not supported — use println for those.

import @fmt
do main() {
mut score i64 = 42
mut x i64 = 7
fmt.printf("%-10s %5d\n", {"score", score}) // "score 42"
fmt.printf("%08.2f\n", {3.14159}) // "00003.14"
mut s string = fmt.sprintf("x = %d", {x}) // "x = 7"
mut parts [i64] = {2026, 9, 28}
fmt.printfln("%d-%02d-%02d", parts) // "2026-09-28"
}
Function Signature Description
pad_left (s string, width i64, ch char) -> string Pad string on the left to width with ch
pad_right (s string, width i64, ch char) -> string Pad string on the right to width with ch
center (s string, width i64, ch char) -> string Center string within width, padding both sides with ch
Function Signature Description
i64_to_hex (n i64) -> string Format integer as lowercase hexadecimal (no 0x prefix)
i64_to_binary (n i64) -> string Format integer as binary
i64_to_octal (n i64) -> string Format integer as octal
f64_to_fixed (f f64, decimals i64) -> string Format f64 with fixed decimal places
f64_to_scientific (f f64) -> string Format f64 in scientific notation
format_number (n i64) -> string Decimal string with ASCII comma thousands separators (1234567 → "1,234,567", -1000 → "-1,000")
format_bytes (n i64) -> string Human-readable byte count in binary units B/KiB/MiB/GiB/TiB/PiB; whole bytes below 1024 ("1023 B"), one decimal above ("1.5 KiB")

Formatted output functions take one array element per format directive; each is independently i64, u64, f64, string, bool, char, or a bigint (i128/u128/i256/u256, integer directives only). Composite types (structs, arrays, maps) are not supported. Use println for printing composite types.