Functional Weave
Code in TypeScript

form.money-input

Read a typed amount as minor units for its currency and locale, check min/max, and format it back.

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

Pinned by 61 tests, run in TypeScript, Python and Rust.parseMoneyInput 37 · formatMoneyInput 13 · moneyInputFormat 11

What it does

The logic behind a money input: read what was typed as integer minor units, check it, and write an amount back for editing, the same way in the browser (`react.form.money-field` calls it) and on the server (TypeScript, Python or Rust), so both refuse the same text with the same words.

parseMoneyInput("£1,234.50", "GBP", "en-GB", { label: "Monthly rent", required: true, min: 1000, max: 500000, allowNegative: false });
// { valid: true, amount: { minor: 123450, currency: "GBP" }, message: null }

parseMoneyInput("1,234.50", "EUR", "de-DE", { label: "Amount", required: true, min: null, max: null, allowNegative: false });
// { valid: false, amount: null, message: "Enter amount in the correct format, like 1.234,50" }

formatMoneyInput({ minor: 123450, currency: "EUR" }, "fr-FR");   // "1 234,50"
moneyInputFormat("SEK", "sv-SE");
// { currency: "SEK", symbol: "kr", symbolBefore: false, digits: 2, groupSeparator: " ", decimalSeparator: ",", example: "1 234,50" }

The functions

A group: 3 functions that work together, each in its own file, each pinned by its own tests in TypeScript, Python and Rust. A project can install only the ones it calls.

  1. parseMoneyInput (text: string, currency: string, locale: string, rules: MoneyRules) -> MoneyInputCheck
  2. formatMoneyInput (amount: Money, locale: string) -> string
  3. moneyInputFormat (currency: string, locale: string) -> MoneyInputFormat

The types it declares, generated into your project

/** What a money input requires. */
export interface MoneyRules {
  /** what the amount is, as it starts a sentence: "Amount", "Monthly rent" */
  readonly label: string;
  /** an empty value is an error */
  readonly required: boolean;
  /** the smallest amount allowed, in minor units */
  readonly min: number | null;
  /** the largest amount allowed, in minor units */
  readonly max: number | null;
  /** a leading minus is accepted */
  readonly allowNegative: boolean;
}

/** The verdict on a money input. */
export interface MoneyInputCheck {
  readonly valid: boolean;
  /** the amount, when valid and given */
  readonly amount: Money | null;
  /** the error message to show, when not valid */
  readonly message: string | null;
}

/** How a money input for a currency and locale looks. */
export interface MoneyInputFormat {
  /** the ISO code */
  readonly currency: string;
  /** the symbol to show beside the input, such as £ or € */
  readonly symbol: string;
  /** the symbol goes before the amount (a prefix) rather than after (a suffix) */
  readonly symbolBefore: boolean;
  /** decimal places in the minor unit: 2 for GBP, 0 for JPY */
  readonly digits: number;
  /** what groups thousands in this locale */
  readonly groupSeparator: string;
  /** what marks the decimal in this locale */
  readonly decimalSeparator: string;
  /** 1234.50 written this way, for a hint: "1,234.50" */
  readonly example: string;
}

Once installed, your code imports each one from the group's module.

parseMoneyInput throws on bad input 37 tests

export function parseMoneyInput(text: string, currency: string, locale: string, rules: MoneyRules): MoneyInputCheck
textstringwhat was typed: "1,234.50", "£12", "1.234,50" for de-DE; untrimmed
currencystringISO 4217 code; a symbol typed in the text must be this currency's
localestringBCP 47 locale (en-GB, de-DE, fr-FR...) that says how digits are grouped and the decimal marked
rulesMoneyRulesthe label, required, and optional bounds in minor units
returnsMoneyInputCheckvalid with the Money, or the message

For example

  • parseMoneyInput(1,234.50, GBP, en-GB, label Amount, required true, min —, max —, allow negative false) → valid true, amount £1,234.50, message — grouped pounds and pence
  • parseMoneyInput(£1,234.50, GBP, en-GB, label Amount, required true, min —, max —, allow negative false) → valid true, amount £1,234.50, message — the pound sign typed before it
  • parseMoneyInput( £12 , GBP, en-GB, label Amount, required true, min —, max —, allow negative false) → valid true, amount £12.00, message — spaces and a trailing newline are trimmed
import { parseMoneyInput } from "#fune/form.money-input@^1";
impl/typescript/parse_money_input.ts · 145 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 { currencyDigits } from "./money_currency_digits.ts";  ← from money.currency-digits ^1.0.0 · built alongside by fune
import { CURRENCY_DIGITS } from "./money_currency_digits_data.ts";  ← money.currency-digits’s rule data (^1.0.0) · built alongside by fune
import { CURRENCIES } from "./money_format_data.ts";  ← money.format’s rule data (^1.0.0) · built alongside by fune
import { parseMoney } from "./money_parse.ts";  ← from money.parse ^1.0.0 · built alongside by fune
import { CURRENCY_SYMBOLS } from "./money_parse_data.ts";  ← money.parse’s rule data (^1.0.0) · built alongside by fune
import { numberStyleFor, separatorsOf } from "./form_money_input_format_money_input.ts";  ← formatMoneyInput, another function of this group · built into the same file, even by a slim install
import { moneyForMessage, moneyInputFormat } from "./form_money_input_money_input_format.ts";  ← moneyInputFormat, another function of this group · built into the same file, even by a slim install
import type { MoneyInputCheck, MoneyRules } from "./form_money_input_types.ts";

const MAX_SAFE = "9007199254740991";
// The spaces money.parse allows between a symbol and the number.
const SPACES = [" ", " ", " "];

function isSpace(ch: string): boolean {
  return ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === "\f" || ch === "\v";
}

function trim(value: string): string {
  let start = 0;
  let end = value.length;
  while (start < end && isSpace(value[start])) start++;
  while (end > start && isSpace(value[end - 1])) end--;
  return value.slice(start, end);
}

function isDigits(value: string): boolean {
  if (value === "") return false;
  for (const ch of value) if (ch < "0" || ch > "9") return false;
  return true;
}

// "Monthly rent" starts a sentence; after "Enter" it is "monthly rent". A
// label that starts with an acronym ("VAT amount") is left as it is.
function lowerFirst(label: string): string {
  const upper = (c: string | undefined) => c !== undefined && c >= "A" && c <= "Z";
  if (upper(label[0]) && !upper(label[1])) return label[0].toLowerCase() + label.slice(1);
  return label;
}

/** The longest known symbol or ISO code `matches` accepts, as money.parse looks for one. */
function longestSymbol(matches: (symbol: string) => boolean): string | null {
  let best: string | null = null;
  const candidates = [...CURRENCY_SYMBOLS.map((r) => r.symbol), ...CURRENCY_DIGITS.map((r) => r.code)];
  for (const symbol of candidates) {
    if (matches(symbol) && (best === null || symbol.length > best.length)) best = symbol;
  }
  return best;
}

/** Whole and fraction digits, grouped as money.parse accepts them, or null. */
function splitNumber(s: string, groups: string[], decimal: string): [string, string] | null {
  const parts = s.split(decimal);
  if (parts.length > 2) return null;
  const fraction = parts.length === 2 ? parts[1] : "";
  if (parts.length === 2 && !isDigits(fraction)) return null;
  const chunks: string[] = [""];
  for (const ch of parts[0]) {
    if (groups.includes(ch)) chunks.push("");
    else chunks[chunks.length - 1] += ch;
  }
  if (!chunks.every(isDigits)) return null;
  if (chunks.length > 1) {
    const [first, ...rest] = chunks;
    if (first.length > 3 || first.startsWith("0")) return null;
    if (!rest.every((c) => c.length === 3)) return null;
  }
  return [chunks.join(""), fraction];
}

function invalid(message: string): MoneyInputCheck {
  return { valid: false, amount: null, message };
}

/**
 * An amount typed into a money input, read as integer minor units. The shape
 * is checked here first, with a message for each way it can be wrong, so that
 * money.parse (which does the conversion) is only ever given text it accepts;
 * then the bounds.
 */
export function parseMoneyInput(text: string, currency: string, locale: string, rules: MoneyRules): MoneyInputCheck {
  const label = trim(rules.label);
  if (label === "") throw new RangeError('a money input needs a label, such as "Amount", to write its error messages');
  const digits = currencyDigits(currency);
  const style = numberStyleFor(locale);
  if (rules.min != null && rules.max != null && rules.min > rules.max) {
    throw new RangeError(`rules.min (${rules.min}) must not be more than rules.max (${rules.max})`);
  }
  const { groups, decimal } = separatorsOf(style);

  let s = trim(text);
  if (s === "") return rules.required ? invalid(`Enter ${lowerFirst(label)}`) : { valid: true, amount: null, message: null };
  const shape = `Enter ${lowerFirst(label)} in the correct format, like ${moneyInputFormat(currency, locale).example}`;

  let negative = false;
  if (s.startsWith("-")) {
    negative = true;
    s = s.slice(1);
  }
  let symbol = longestSymbol((k) => s.startsWith(k));
  if (symbol !== null) {
    s = s.slice(symbol.length);
    if (SPACES.includes(s.charAt(0))) s = s.slice(1);
    if (!negative && s.startsWith("-")) {
      negative = true;
      s = s.slice(1);
    }
  } else {
    symbol = longestSymbol((k) => s.endsWith(k));
    if (symbol !== null) {
      s = s.slice(0, s.length - symbol.length);
      if (SPACES.includes(s.charAt(s.length - 1))) s = s.slice(0, -1);
    }
  }
  // Another currency's symbol is a different amount, not a typo to forgive.
  const own =
    symbol === null ||
    symbol === currency ||
    CURRENCY_SYMBOLS.some((r) => r.code === currency && r.symbol === symbol) ||
    CURRENCIES.some((r) => r.code === currency && r.symbol.replace(/^ +| +$/g, "") === symbol);
  if (!own) return invalid(shape);

  const number = splitNumber(s, groups, decimal);
  if (number === null) return invalid(shape);
  const [whole, fraction] = number;
  if (negative && !rules.allowNegative) return invalid(`${label} must not be negative`);
  if (fraction.length > digits) {
    if (digits === 0) return invalid(`${label} must be a whole number`);
    return invalid(`${label} must not have more than ${digits} decimal ${digits === 1 ? "place" : "places"}`);
  }
  const all = (whole + fraction.padEnd(digits, "0")).replace(/^0+/, "");
  if (all.length > MAX_SAFE.length || (all.length === MAX_SAFE.length && all > MAX_SAFE)) return invalid(`${label} is too large`);

  const amount = parseMoney((negative ? "-" : "") + whole + (fraction === "" ? "" : decimal + fraction), currency, style as Parameters<typeof parseMoney>[2]);
  const { min, max } = rules;
  if (min != null && max != null) {
    if (amount.minor < min || amount.minor > max) {
      return invalid(`${label} must be between ${moneyForMessage(min, currency, locale)} and ${moneyForMessage(max, currency, locale)}`);
    }
  } else if (min != null && amount.minor < min) {
    return invalid(`${label} must be ${moneyForMessage(min, currency, locale)} or more`);
  } else if (max != null && amount.minor > max) {
    return invalid(`${label} must be ${moneyForMessage(max, currency, locale)} or less`);
  }
  return { valid: true, amount, message: null };
}

formatMoneyInput throws on bad input 13 tests

export function formatMoneyInput(amount: Money, locale: string): string
amountMoneythe amount to put back into the input for editing
localestringhow to group digits and mark the decimal
returnsstringdigits with grouping and every decimal place, no symbol: "1,234.50", "1.234,50", "-0.05"

For example

  • formatMoneyInput(£1,234.50, en-GB) → 1,234.50 pounds and pence
  • formatMoneyInput(€1,234.50, de-DE) → 1.234,50 German
  • formatMoneyInput(€1,234.50, fr-FR) → 1 234,50 French, with a plain space to type
import { formatMoneyInput } from "#fune/form.money-input@^1";
impl/typescript/format_money_input.ts · 72 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 { type Money } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune
import { currencyDigits } from "./money_currency_digits.ts";  ← from money.currency-digits ^1.0.0 · built alongside by fune
import { LOCALES } from "./form_money_input_data.ts";  ← this capability’s own data, compiled from data/locales.json into the same file by fune build

export interface NumberSeparators {
  /** Every character money.parse accepts as a group separator in this style. */
  groups: string[];
  /** The one this capability writes. */
  group: string;
  decimal: string;
}

// ASCII only, so all three languages fold the same characters.
function lower(value: string): string {
  return value.replace(/[A-Z]/g, (c) => c.toLowerCase());
}

/**
 * The money.parse number style for a locale: the exact locale first
 * (case-insensitive, "_" read as "-"), then its language alone.
 */
export function numberStyleFor(locale: string): string {
  const wanted = lower(locale.replace(/_/g, "-"));
  const exact = LOCALES.find((row) => lower(row.locale) === wanted);
  if (exact !== undefined) return exact.style;
  const language = wanted.split("-")[0];
  const general = LOCALES.find((row) => lower(row.locale) === language);
  if (general !== undefined && language !== "") return general.style;
  throw new RangeError(`unknown locale "${locale}": give a BCP 47 locale this capability lists, such as en-GB, de-DE or fr-FR`);
}

/** The separators of a money.parse number style, as money.parse reads them. */
export function separatorsOf(style: string): NumberSeparators {
  switch (style) {
    case "comma-dot":
      return { groups: [","], group: ",", decimal: "." };
    case "dot-comma":
      return { groups: ["."], group: ".", decimal: "," };
    case "space-comma":
      // Space, no-break space and narrow no-break space; a person types the first.
      return { groups: [" ", " ", " "], group: " ", decimal: "," };
    case "apostrophe-dot":
      return { groups: ["'", "’"], group: "'", decimal: "." };
    default:
      throw new RangeError(`unknown number style "${style}"`);
  }
}

/** Minor units written with thousands grouping and every decimal place, no symbol. */
export function writeAmount(minor: number, digits: number, style: string): string {
  const { group, decimal } = separatorsOf(style);
  const raw = String(Math.abs(minor)).padStart(digits + 1, "0");
  const whole = raw.slice(0, raw.length - digits);
  const fraction = raw.slice(raw.length - digits);
  let grouped = "";
  for (let i = 0; i < whole.length; i++) {
    if (i > 0 && (whole.length - i) % 3 === 0) grouped += group;
    grouped += whole[i];
  }
  const number = digits > 0 ? grouped + decimal + fraction : grouped;
  return minor < 0 ? "-" + number : number;
}

/**
 * An amount as a money input shows it for editing: the locale's grouping and
 * decimal mark, every decimal place the currency has, and no symbol (the
 * input's prefix or suffix shows that). parseMoneyInput reads it back.
 */
export function formatMoneyInput(amount: Money, locale: string): string {
  const digits = currencyDigits(amount.currency);
  return writeAmount(amount.minor, digits, numberStyleFor(locale));
}

moneyInputFormat throws on bad input 11 tests

export function moneyInputFormat(currency: string, locale: string): MoneyInputFormat
currencystringISO 4217 code
localestringBCP 47 locale
returnsMoneyInputFormatwhat a money input shows: the symbol, separators and an example

For example

  • moneyInputFormat(GBP, en-GB) → currency GBP, symbol £, symbol before true, digits 2, group separator ,, decimal separator ., example 1,234.50 pounds in the UK
  • moneyInputFormat(EUR, de-DE) → currency EUR, symbol €, symbol before true, digits 2, group separator ., decimal separator ,, example 1.234,50 euros in Germany
  • moneyInputFormat(SEK, sv-SE) → currency SEK, symbol kr, symbol before false, digits 2, group separator , decimal separator ,, example 1 234,50 kronor in Sweden: the symbol goes after
import { moneyInputFormat } from "#fune/form.money-input@^1";
impl/typescript/money_input_format.ts · 55 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 { currencyDigits } from "./money_currency_digits.ts";  ← from money.currency-digits ^1.0.0 · built alongside by fune
import { CURRENCIES } from "./money_format_data.ts";  ← money.format’s rule data (^1.0.0) · built alongside by fune
import { CURRENCY_SYMBOLS } from "./money_parse_data.ts";  ← money.parse’s rule data (^1.0.0) · built alongside by fune
import { numberStyleFor, separatorsOf, writeAmount } from "./form_money_input_format_money_input.ts";  ← formatMoneyInput, another function of this group · built into the same file, even by a slim install
import type { MoneyInputFormat } from "./form_money_input_types.ts";

function trimSpaces(value: string): string {
  return value.replace(/^ +| +$/g, "");
}

/**
 * The symbol for a currency and whether it goes first: money.format's table
 * where it has the currency, else the first symbol money.parse knows for it
 * (before the amount), else the ISO code itself. `raw` keeps money.format's
 * own spacing ("CHF ", " kr"), for writing an amount in a sentence.
 */
export function currencySymbol(currency: string): { symbol: string; raw: string; before: boolean } {
  const known = CURRENCIES.find((row) => row.code === currency);
  if (known !== undefined) return { symbol: trimSpaces(known.symbol), raw: known.symbol, before: known.symbolBefore };
  const parsed = CURRENCY_SYMBOLS.find((row) => row.code === currency);
  if (parsed !== undefined) return { symbol: parsed.symbol, raw: parsed.symbol, before: true };
  return { symbol: currency, raw: currency + " ", before: true };
}

/** An amount in a sentence, such as an error message: "£10.00", "100,00 kr", "€1.000,00". */
export function moneyForMessage(minor: number, currency: string, locale: string): string {
  const digits = currencyDigits(currency);
  const { raw, before } = currencySymbol(currency);
  const number = writeAmount(Math.abs(minor), digits, numberStyleFor(locale));
  const body = before ? raw + number : number + raw;
  return minor < 0 ? "-" + body : body;
}

/**
 * What a money input for this currency and locale shows: the symbol beside
 * the input, the separators, and 1234.50 written the locale's way (with as
 * many decimal places as the currency has: "1,234" for yen, "1,234.500" for
 * Kuwaiti dinar) for a hint such as "For example, 1,234.50".
 */
export function moneyInputFormat(currency: string, locale: string): MoneyInputFormat {
  const digits = currencyDigits(currency);
  const style = numberStyleFor(locale);
  const { group, decimal } = separatorsOf(style);
  const { symbol, before } = currencySymbol(currency);
  const example = Number("1234" + "50".padEnd(digits, "0").slice(0, digits));
  return {
    currency,
    symbol,
    symbolBefore: before,
    digits,
    groupSeparator: group,
    decimalSeparator: decimal,
    example: writeAmount(example, digits, style),
  };
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 4 dependencies, 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 form.money-input

That builds the whole group. To build only what you call, and whatever it uses inside the group:

fune add form.money-input --only parseMoneyInput
Download for TypeScript form.money-input-1.0.0-typescript.fune · 44,809 bytes sha256 122db713d858bf3e731ade094afcbff58aaf83881a7542b55ad3f657d67b4721

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

The whole function, every language, is one file too: form.money-input-1.0.0.fune, 71,173 bytes, sha256 c6e008a8d83dc36f54bfae7d3b176420ea843ee74067b2f46591a6408f12ed42. 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 form.money-input.parseMoneyInput
// fune: before form.money-input.formatMoneyInput
// fune: before form.money-input.moneyInputFormat

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

// fune: after form.money-input.parseMoneyInput
// fune: after form.money-input.formatMoneyInput
// fune: after form.money-input.moneyInputFormat

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 form.money-input
// fune: replace money.currency-digits in form.money-input
// fune: replace money.format in form.money-input
// fune: replace money.parse in form.money-input

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

// fune: step form.money-input.<fn> 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.

parseMoneyInput 37 tests

CaseArgumentsExpected
grouped pounds and pence 1,234.50, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount £1,234.50, message —
the pound sign typed before it £1,234.50, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount £1,234.50, message —
spaces and a trailing newline are trimmed £12 , GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount £12.00, message —
one decimal place is enough: 1234.5 is £1,234.50 1234.5, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount £1,234.50, message —
German grouping and decimal comma 1.234,50, EUR, de-DE, label Amount, required true, min —, max —, allow negative false → valid true, amount €1,234.50, message —
French spaces and a trailing euro sign 1 234,50 €, EUR, fr-FR, label Amount, required true, min —, max —, allow negative false → valid true, amount €1,234.50, message —
Swiss apostrophes 1'234.50, CHF, de-CH, label Amount, required true, min —, max —, allow negative false → valid true, amount 1,234.50 CHF, message —
Swedish kronor written after the amount 100 kr, SEK, sv-SE, label Amount, required true, min —, max —, allow negative false → valid true, amount 100.00 SEK, message —
the ISO code typed as the symbol GBP 12, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount £12.00, message —
yen have no decimal places 1,500, JPY, ja-JP, label Amount, required true, min —, max —, allow negative false → valid true, amount ¥1,500, message —
Show the other 27 tests
CaseArgumentsExpected
Kuwaiti dinar have three 1.5, KWD, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount 1.500 KWD, message —
a locale's language alone, and an underscore 1.234,50, EUR, de_AT, label Amount, required true, min —, max —, allow negative false → valid true, amount €1,234.50, message —
nothing typed in a required input , GBP, en-GB, label Monthly rent, required true, min —, max —, allow negative false → valid false, amount —, message Enter monthly rent
nothing typed in an optional input is valid, with no amount , GBP, en-GB, label Amount, required false, min —, max —, allow negative false → valid true, amount —, message —
another currency's symbol $12, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Enter amount in the correct format, like 1,234.50
English grouping in a German input is wrong, and the example says how 1,234.50, EUR, de-DE, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Enter amount in the correct format, like 1.234,50
grouping that is not thousands 1,23.45, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Enter amount in the correct format, like 1,234.50
Arabic-Indic digits are not an amount ١٢, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Enter amount in the correct format, like 1,234.50
letters twelve, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Enter amount in the correct format, like 1,234.50
too many decimal places 12.345, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Amount must not have more than 2 decimal places
decimals in a currency without them 1500.5, JPY, ja-JP, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Amount must be a whole number
a minus where negatives are not allowed -5, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Amount must not be negative
a refund may be negative, the minus before the symbol -£5.00, GBP, en-GB, label Amount, required true, min —, max —, allow negative true → valid true, amount -£5.00, message —
the minus after the symbol £-0.05, GBP, en-GB, label Amount, required true, min —, max —, allow negative true → valid true, amount -£0.05, message —
the largest amount money holds exactly 90071992547409.91, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid true, amount £90,071,992,547,409.91, message —
one penny more is too large 90071992547409.92, GBP, en-GB, label Amount, required true, min —, max —, allow negative false → valid false, amount —, message Amount is too large
below the range, both bounds in the message 5, GBP, en-GB, label Monthly rent, required true, min 1,000, max 100,000, allow negative false → valid false, amount —, message Monthly rent must be between £10.00 and £1,000.00
exactly the minimum is allowed 10, GBP, en-GB, label Amount, required true, min 1,000, max 100,000, allow negative false → valid true, amount £10.00, message —
a minimum only 5, GBP, en-GB, label Amount, required true, min 1,000, max —, allow negative false → valid false, amount —, message Amount must be £10.00 or more
a maximum only, the bound written the German way 2.000, EUR, de-DE, label Amount, required true, min —, max 100,000, allow negative false → valid false, amount —, message Amount must be €1.000,00 or less
a bound in kronor, symbol after 5, SEK, sv-SE, label Amount, required true, min 10,000, max —, allow negative false → valid false, amount —, message Amount must be 100,00 kr or more
a currency money.format has no symbol for is named by its code 1, BHD, en-GB, label Amount, required true, min 10,000, max —, allow negative false → valid false, amount —, message Amount must be BHD 10.000 or more
an acronym label keeps its capital , GBP, en-GB, label VAT amount, required true, min —, max —, allow negative false → valid false, amount —, message Enter VAT amount
an unknown locale 1, GBP, xx-YY, label Amount, required true, min —, max —, allow negative false → error: unknown locale "xx-YY"
an unknown currency 1, ABC, en-GB, label Amount, required true, min —, max —, allow negative false → error: no ISO 4217 minor unit for currency "ABC"
min above max 1, GBP, en-GB, label Amount, required true, min 500, max 100, allow negative false → error: rules.min (500) must not be more than rules.max (100)
an empty label 1, GBP, en-GB, label , required true, min —, max —, allow negative false → error: a money input needs a label

formatMoneyInput 13 tests

CaseArgumentsExpected
pounds and pence £1,234.50, en-GB → 1,234.50
German €1,234.50, de-DE → 1.234,50
French, with a plain space to type €1,234.50, fr-FR → 1 234,50
Swiss 1,234.50 CHF, de-CH → 1'234.50
a negative penny -£0.05, en-GB → -0.05
zero keeps its decimals £0.00, en-GB → 0.00
yen: no decimal mark ¥1,500, ja-JP → 1,500
three decimal places 1,234.567 KWD, en-GB → 1,234.567
a million dollars $1,000,000.00, en-US → 1,000,000.00
a language alone £9.99, en → 9.99
Show the other 3 tests
CaseArgumentsExpected
the largest exact amount £90,071,992,547,409.91, en-GB → 90,071,992,547,409.91
an unknown locale £0.01, tlh → error: unknown locale "tlh"
an unknown currency 0.01 ABC, en-GB → error: no ISO 4217 minor unit for currency "ABC"

moneyInputFormat 11 tests

CaseArgumentsExpected
pounds in the UK GBP, en-GB → currency GBP, symbol £, symbol before true, digits 2, group separator ,, decimal separator ., example 1,234.50
euros in Germany EUR, de-DE → currency EUR, symbol €, symbol before true, digits 2, group separator ., decimal separator ,, example 1.234,50
kronor in Sweden: the symbol goes after SEK, sv-SE → currency SEK, symbol kr, symbol before false, digits 2, group separator , decimal separator ,, example 1 234,50
francs in Switzerland: the symbol trimmed for the prefix CHF, de-CH → currency CHF, symbol CHF, symbol before true, digits 2, group separator ', decimal separator ., example 1'234.50
yen: no decimals in the example JPY, ja-JP → currency JPY, symbol ¥, symbol before true, digits 0, group separator ,, decimal separator ., example 1,234
Kuwaiti dinar: three KWD, en-GB → currency KWD, symbol KD, symbol before true, digits 3, group separator ,, decimal separator ., example 1,234.500
Canadian dollars: money.parse's first symbol CAD, en-CA → currency CAD, symbol $, symbol before true, digits 2, group separator ,, decimal separator ., example 1,234.50
Bahraini dinar: no symbol known, so the code BHD, en-GB → currency BHD, symbol BHD, symbol before true, digits 3, group separator ,, decimal separator ., example 1,234.500
euros in France EUR, fr-FR → currency EUR, symbol €, symbol before true, digits 2, group separator , decimal separator ,, example 1 234,50
an unknown locale GBP, → error: unknown locale ""
Show the other 1 test
CaseArgumentsExpected
an unknown currency gbp, en-GB → error: no ISO 4217 minor unit for currency "gbp"

Verification

Verified by the operator, not the sandbox 61 test vectors declared; none run by the registry.

Verified locally by the registry's operator, not by the sandbox. This version was published with FUNE_VERIFY_MODE=inline-dev (the operator's import of the first-party registry, or a development server): the server validated the package - its manifest, signatures, imports, safety lint and test rules - but the registry did not run its test vectors in its sandbox. The operator runs them with `fune verify --all` before importing.

Signed by the registry with key fw-9dc887227da47575 (ed25519), over its integrity and this report's digest. The full report · JSON

More from the author

## Built on money.*

- **money.parse** does the conversion to minor units. It is strict and throws; a form wants a message instead, so this capability checks the shape first, with money.parse's own rules (symbol before or after, at most one space beside it, a minus before or after a leading symbol, real thousands grouping or none), and hands money.parse only text it is certain to accept. - **money.currency-digits** says how many decimal places a currency has (2 for GBP, 0 for JPY, 3 for KWD). Unknown currencies throw its error. - **money.format**'s currency table gives the symbol and which side it goes on; for a currency it does not list, the first symbol money.parse knows for it (put before), else the ISO code. Amounts in messages are written with that symbol and the locale's separators: "£10.00", "€1.000,00", "100,00 kr".

## Locales

`locale` picks money.parse's number style from `data/locales.json`: the exact locale first (case-insensitive, `_` read as `-`), then its language alone (`de-AT` is listed; `de-LU` falls back to `de`). An unlisted locale throws `unknown locale`. The styles are comma-dot (en, ja, zh, ko, he, th), dot-comma (de, es, it, nl, pt-BR, da, id, tr, el), space-comma (fr, pt-PT, sv, nb, fi, pl, cs, sk, hu, ru, uk) and apostrophe-dot (de-CH, it-CH, de-LI).

Only thousands grouping is read and written: en-IN's lakh grouping (12,34,567.00) is refused as typed, though the ungrouped number is accepted. `formatMoneyInput` groups with a plain space in space-comma locales (what a keyboard types); money.parse also reads the no-break spaces documents use.

## The checks, in order, and their messages

| Typed | Message | |---|---| | nothing, `required` | Enter monthly rent | | not an amount in this locale, or another currency's symbol | Enter monthly rent in the correct format, like 1,234.50 | | a minus, without `allowNegative` | Monthly rent must not be negative | | too many decimals | Monthly rent must not have more than 2 decimal places | | decimals for a currency without them | Monthly rent must be a whole number | | more than 2^53 - 1 minor units | Monthly rent is too large | | outside `min`..`max` (minor units, inclusive) | Monthly rent must be between £10.00 and £5,000.00 / must be £10.00 or more / must be £5,000.00 or less |

Text is trimmed of ASCII whitespace; digits are ASCII only, so "١٢" is not an amount. Nothing typed in an optional input is valid with `amount` null. The label starts the sentence ("Monthly rent must ..."), and after "Enter" its first letter is lower-cased unless it starts an acronym ("Enter VAT amount").

The example in `moneyInputFormat` and in the format message is 1234.50 with as many decimal places as the currency has: "1,234" for yen, "1,234.500" for Kuwaiti dinar.

## Errors

Thrown, since they are the caller's: an empty label, an unknown currency or locale, `min` above `max`.

Files

PathBytes
README.md3,904
data/locales.json3,049
impl/python/format_money_input.py2,532
impl/python/money_input_format.py2,097
impl/python/parse_money_input.py5,872
impl/rust/format_money_input.rs3,168
impl/rust/money_input_format.rs3,229
impl/rust/parse_money_input.rs8,210
impl/typescript/format_money_input.ts2,993
impl/typescript/money_input_format.ts2,562
impl/typescript/parse_money_input.ts6,408
vectors.json15,025