Functional Weave
Code in TypeScript

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

  • formatMoney(£1,932.99) → £1,932.99 pounds and pence
  • formatMoney(£1,234,567.89) → £1,234,567.89 groups thousands
  • formatMoney(£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.

export function formatMoney(amount: Money): string
amountMoney
returnsstring

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

import { formatMoney } from "#fune/money.format@^1";
impl/typescript.ts · 31 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.

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

/**
 * 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.
 */
export function formatMoney(amount: Money): string {
  const format = CURRENCIES.find((c) => c.code === amount.currency);
  if (format === undefined) {
    throw new RangeError(`no formatting rules for currency "${amount.currency}"`);
  }

  const negative = amount.minor < 0;
  const digits = Math.abs(amount.minor).toString().padStart(format.digits + 1, "0");
  const whole = digits.slice(0, digits.length - format.digits);
  const fraction = digits.slice(digits.length - format.digits);

  let grouped = "";
  for (let i = 0; i < whole.length; i++) {
    if (i > 0 && (whole.length - i) % 3 === 0) grouped += format.groupSeparator;
    grouped += whole[i];
  }

  const number = format.digits > 0 ? grouped + format.decimalSeparator + fraction : grouped;
  const body = format.symbolBefore ? format.symbol + number : number + format.symbol;
  return negative ? "-" + body : body;
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the TypeScript 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 TypeScript money.format-1.0.0-typescript.fune · 5,799 bytes sha256 352a8d2ebe98113af0dacc87de82ba297b42451835ca29c8ac63f7dd08d7c5d2

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

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