sprintf functions
1 Overview
2 Exports
sprintf
sprintf*
3 Format String Syntax
4 Supported Conversion Types
5 Flags
6 Field Width
7 Dynamic Width and Precision
8 Precision
9 Length Modifiers
10 Type Checking and Errors
11 Implementation Notes
9.1

sprintf functions🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

 (require sprintf)

This module provides a compact printf-style string formatting facility for Racket. It is inspired by C-style format strings, but implemented on top of racket/format.

The core functionality is provided by sprintf and sprintf*.

1 Overview🔗ℹ

The module exports two formatting functions:

  • sprintf — formats using variadic arguments

  • sprintf* — formats using an explicit argument list

Both functions use the same format-string syntax and semantics.

2 Exports🔗ℹ

procedure

(sprintf format arg ...)  string?

  format : string?
  arg : any/c
Formats the string format by substituting the given arguments according to printf-style conversion specifications.

Arguments are consumed sequentially from left to right. An error is raised if there are too many or too few arguments for the format string.

procedure

(sprintf* format args)  string?

  format : string?
  args : list?
Variant of sprintf where the arguments are supplied explicitly as a list.

This form is useful when arguments are constructed dynamically or passed through higher-order functions.

3 Format String Syntax🔗ℹ

A format specification has the general form:

%[flags][width][.precision][length]type

Only a well-defined subset of the traditional printf syntax is supported.

4 Supported Conversion Types🔗ℹ

The following conversion specifiers are recognized:

  • %d — decimal integer

  • %x — hexadecimal integer

  • %f — floating-point number

  • %s — string

  • %% — literal percent sign

5 Flags🔗ℹ

The following flags are supported:

  • 0 — pad numeric output with zeros instead of spaces

  • - — left-align the result within the field width

If no flag is specified, numeric values are right-aligned and padded with spaces.

6 Field Width🔗ℹ

A minimum field width may be specified as a decimal number:

%8d

%-10s

If the formatted value is shorter than the field width, it is padded according to the alignment rules.

7 Dynamic Width and Precision🔗ℹ

Both width and precision may be specified using *. In that case, the value is taken from the argument list.

Example:

(sprintf "%*.*f" 8 3 1.23456)

In this example:
  • the first argument specifies the field width

  • the second argument specifies the precision

  • the third argument is the value to be formatted

The arguments supplying width or precision must be numbers; otherwise an error is raised.

8 Precision🔗ℹ

Precision has different meanings depending on the conversion type:

  • for numeric conversions, it controls the number of digits after the decimal point

  • for strings, it specifies the maximum output width

If no precision is specified:
  • numeric values default to precision 0 for integers

  • strings are not truncated

9 Length Modifiers🔗ℹ

The length modifier field (e.g. l) is parsed for syntactic compatibility but otherwise ignored. It has no semantic effect.

10 Type Checking and Errors🔗ℹ

The formatter performs runtime type checking:

  • numeric conversion specifiers require numeric arguments

  • %s requires a string argument

Errors are raised in the following situations:

  • arguments remain but no format specifiers are left

  • format specifiers remain but no arguments are left

  • * is used but no corresponding numeric argument is available

  • argument types do not match the conversion specifier

Error messages are designed to be explicit and descriptive.

11 Implementation Notes🔗ℹ

Internally, the formatter uses regular-expression driven parsing and delegates the actual formatting to ~r and ~a from racket/format.

The implementation is intentionally strict: mismatches between format strings and arguments are treated as programming errors and reported immediately.