Functional Weave
Code in Python

money.format

Render a monetary amount as text, using the currency's real minor-unit precision.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 11 tests, run in TypeScript, Python and Rust.

What it does

Not every currency has two decimal places. Japanese yen has none and Kuwaiti dinar has three, so dividing by 100 to display an amount is wrong roughly as often as it is right.

This is deliberately a fixed presentation per currency rather than full locale formatting: it is stable, identical in all three languages, and testable. Real per-locale formatting (which differs by the reader's locale, not the money's currency) belongs in a separate capability that can afford to be locale-aware.

For example

  • format_money(£1,932.99) → £1,932.99 pounds and pence
  • format_money(£1,234,567.89) → £1,234,567.89 groups thousands
  • format_money(£0.05) → £0.05 under a pound keeps the leading zero

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def format_money(amount: Money) -> str
amountMoney
returnsstring

Your code names it in one line, in the file that uses it

from fune.money.format import format_money  # money.format@^1
impl/python.py · 29 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .money_amount import Money  ← from money.amount ^1.0.0 · built alongside by fune
from .money_format_data import CURRENCIES  ← this capability’s own data, compiled from data/currencies.json into the same file by fune build


def format_money(amount: Money) -> str:
    """Render a monetary amount as text.

    Not every currency has two decimal places: yen has none, Kuwaiti dinar has
    three. Dividing by 100 to display an amount is wrong about as often as it
    is right, so the precision comes from the currency table.
    """
    fmt = next((c for c in CURRENCIES if c.code == amount.currency), None)
    if fmt is None:
        raise ValueError('no formatting rules for currency "%s"' % (amount.currency,))

    negative = amount.minor < 0
    digits = str(abs(amount.minor)).rjust(fmt.digits + 1, "0")
    whole = digits[: len(digits) - fmt.digits]
    fraction = digits[len(digits) - fmt.digits :]

    grouped = ""
    for i, ch in enumerate(whole):
        if i > 0 and (len(whole) - i) % 3 == 0:
            grouped += fmt.group_separator
        grouped += ch

    number = grouped + fmt.decimal_separator + fraction if fmt.digits > 0 else grouped
    body = fmt.symbol + number if fmt.symbol_before else number + fmt.symbol
    return "-" + body if negative else body

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add money.format
Download for Python money.format-1.0.0-python.fune · 5,675 bytes sha256 4747838046bcb2eb7020c43d753786315110620195ccfdd4c154212b072d5353

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./money.format-1.0.0-python.fune, or fetch it from a terminal with fune pull money.format@1.0.0:python.

The whole function, every language, is one file too: money.format-1.0.0.fune, 8,922 bytes, sha256 d0d6f4e61a074c3a58575ea3c268f73feba16804516f27bafeb63b5be66c0138. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before money.format

after — your function gets the result and the arguments, and returns the final result.

# fune: after money.format

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

# fune: replace money.amount in money.format

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show money.format --steps.

# fune: step money.format after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
pounds and pence £1,932.99 → £1,932.99
groups thousands £1,234,567.89 → £1,234,567.89
under a pound keeps the leading zero £0.05 → £0.05
zero £0.00 → £0.00
negative amounts lead with the sign -£10.50 → -£10.50
yen has no minor unit ¥1,500 → ¥1,500
dinar has three 1.500 KWD → KD 1.500
euro €1,230.00 → €1,230.00
swiss grouping 1,234,567.89 CHF → CHF 1'234'567.89
trailing symbol and comma decimal 1,234.56 SEK → 1 234,56 kr
Show the other 1 test
CaseArgumentsExpected
an unknown currency is an error, not a guess 1.00 XYZ → error: no formatting rules

Files

PathBytes
README.md507
data/currencies.json848
impl/python.py1,135
impl/rust.rs1,843
impl/typescript.ts1,251
vectors.json1,252