Skip to main content

fmt.fmt

fmt.fmt(
    quantity,
    unit=None,
    precision=1,
    commas=True,
    prefix='',
    suffix='',
    approx=False,
    lower_bound=False,
    upper_bound=False,
    trim_trailing_zeros=False,
)

Format a Pint Quantity (or plain number) for narrative text. Returns a MarkdownStr so Quarto inserts the value verbatim (no escape).

The prefix and suffix arguments collapse the old MarkdownStr(f”…“) escape-hatch idiom into a single canonical helper. Common uses:

fmt(value, precision=0, approx=True)       # "~1,000"
fmt(value, precision=0, lower_bound=True)  # "> 1,000"
fmt_percent(rate, precision=1, style="symbol")  # "12.4%"
fmt_qty(bandwidth, unit=MB/second)         # "2.4 MB/s"
fmt_multiple(speedup)                      # "3.2×"

Safety: Raises ValueError if formatting would hide meaningful magnitude: non-zero values displayed as 0, non-integers displayed with precision=0, or integer-like values shown with spurious decimals (512.0). Use fmt_int(...) when integer display is intentional.

Back to top