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.
- parse_money_input (text: string, currency: string, locale: string, rules: MoneyRules) -> MoneyInputCheck
- format_money_input (amount: Money, locale: string) -> string
- money_input_format (currency: string, locale: string) -> MoneyInputFormat
The types it declares, generated into your project
@dataclass(frozen=True)
class MoneyRules:
"""What a money input requires."""
#: what the amount is, as it starts a sentence: "Amount", "Monthly rent"
label: str
#: an empty value is an error
required: bool
#: the smallest amount allowed, in minor units
min: Optional[int]
#: the largest amount allowed, in minor units
max: Optional[int]
#: a leading minus is accepted
allow_negative: bool
@dataclass(frozen=True)
class MoneyInputCheck:
"""The verdict on a money input."""
valid: bool
#: the amount, when valid and given
amount: Optional[Money]
#: the error message to show, when not valid
message: Optional[str]
@dataclass(frozen=True)
class MoneyInputFormat:
"""How a money input for a currency and locale looks."""
#: the ISO code
currency: str
#: the symbol to show beside the input, such as £ or €
symbol: str
#: the symbol goes before the amount (a prefix) rather than after (a suffix)
symbol_before: bool
#: decimal places in the minor unit: 2 for GBP, 0 for JPY
digits: int
#: what groups thousands in this locale
group_separator: str
#: what marks the decimal in this locale
decimal_separator: str
#: 1234.50 written this way, for a hint: "1,234.50"
example: str
Once installed, your code imports each one from the group's module.
parse_money_input throws on bad input 37 tests
def parse_money_input(text: str, currency: str, locale: str, rules: MoneyRules) -> MoneyInputCheck
| text | string | what was typed: "1,234.50", "£12", "1.234,50" for de-DE; untrimmed |
| currency | string | ISO 4217 code; a symbol typed in the text must be this currency's |
| locale | string | BCP 47 locale (en-GB, de-DE, fr-FR...) that says how digits are grouped and the decimal marked |
| rules | MoneyRules | the label, required, and optional bounds in minor units |
| returns | MoneyInputCheck | valid with the Money, or the message |
For example
parse_money_input(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 penceparse_money_input(£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 itparse_money_input( £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
from fune.form.money_input import parse_money_input # form.money-input@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import Callable, List, Optional, Tuple
from .form_money_input_format_money_input import number_style_for, separators_of ← formatMoneyInput, another function of this group · built into the same file, even by a slim install
from .form_money_input_money_input_format import money_for_message, money_input_format ← moneyInputFormat, another function of this group · built into the same file, even by a slim install
from .form_money_input_types import MoneyInputCheck, MoneyRules
from .money_currency_digits import currency_digits ← from money.currency-digits ^1.0.0 · built alongside by fune
from .money_currency_digits_data import CURRENCY_DIGITS ← money.currency-digits’s rule data (^1.0.0) · built alongside by fune
from .money_format_data import CURRENCIES ← money.format’s rule data (^1.0.0) · built alongside by fune
from .money_parse import parse_money ← from money.parse ^1.0.0 · built alongside by fune
from .money_parse_data import CURRENCY_SYMBOLS ← money.parse’s rule data (^1.0.0) · built alongside by fune
MAX_SAFE = "9007199254740991"
# The spaces money.parse allows between a symbol and the number.
SPACES = (" ", " ", " ")
# ASCII whitespace only, so all three languages trim the same characters.
_SPACE = " \t\n\r\f\v"
def _is_digits(value: str) -> bool:
return value != "" and all("0" <= ch <= "9" for ch in value)
def _lower_first(label: str) -> str:
def upper(i: int) -> bool:
return i < len(label) and "A" <= label[i] <= "Z"
if upper(0) and not upper(1):
return chr(ord(label[0]) + 32) + label[1:]
return label
def _longest_symbol(matches: Callable[[str], bool]) -> Optional[str]:
best: Optional[str] = None
candidates = [row.symbol for row in CURRENCY_SYMBOLS] + [row.code for row in CURRENCY_DIGITS]
for symbol in candidates:
if matches(symbol) and (best is None or len(symbol) > len(best)):
best = symbol
return best
def _split_number(s: str, groups: List[str], decimal: str) -> Optional[Tuple[str, str]]:
parts = s.split(decimal)
if len(parts) > 2:
return None
fraction = parts[1] if len(parts) == 2 else ""
if len(parts) == 2 and not _is_digits(fraction):
return None
chunks = [""]
for ch in parts[0]:
if ch in groups:
chunks.append("")
else:
chunks[-1] += ch
if not all(_is_digits(c) for c in chunks):
return None
if len(chunks) > 1:
first, rest = chunks[0], chunks[1:]
if len(first) > 3 or first.startswith("0"):
return None
if not all(len(c) == 3 for c in rest):
return None
return "".join(chunks), fraction
def _invalid(message: str) -> MoneyInputCheck:
return MoneyInputCheck(valid=False, amount=None, message=message)
def parse_money_input(text: str, currency: str, locale: str, rules: MoneyRules) -> MoneyInputCheck:
"""An amount typed into a money input, read as integer minor units: the
shape checked here, with a message for each way it can be wrong, so that
money.parse only ever converts text it accepts; then the bounds."""
label = rules.label.strip(_SPACE)
if label == "":
raise ValueError('a money input needs a label, such as "Amount", to write its error messages')
digits = currency_digits(currency)
style = number_style_for(locale)
if rules.min is not None and rules.max is not None and rules.min > rules.max:
raise ValueError("rules.min (%d) must not be more than rules.max (%d)" % (rules.min, rules.max))
seps = separators_of(style)
s = text.strip(_SPACE)
if s == "":
if rules.required:
return _invalid("Enter %s" % _lower_first(label))
return MoneyInputCheck(valid=True, amount=None, message=None)
shape = "Enter %s in the correct format, like %s" % (_lower_first(label), money_input_format(currency, locale).example)
negative = False
if s.startswith("-"):
negative = True
s = s[1:]
symbol = _longest_symbol(lambda k: s.startswith(k))
if symbol is not None:
s = s[len(symbol) :]
if s[:1] in SPACES and s[:1] != "":
s = s[1:]
if not negative and s.startswith("-"):
negative = True
s = s[1:]
else:
symbol = _longest_symbol(lambda k: s.endswith(k))
if symbol is not None:
s = s[: len(s) - len(symbol)]
if s[-1:] in SPACES and s[-1:] != "":
s = s[:-1]
own = (
symbol is None
or symbol == currency
or any(r.code == currency and r.symbol == symbol for r in CURRENCY_SYMBOLS)
or any(r.code == currency and r.symbol.strip(" ") == symbol for r in CURRENCIES)
)
if not own:
return _invalid(shape)
number = _split_number(s, seps.groups, seps.decimal)
if number is None:
return _invalid(shape)
whole, fraction = number
if negative and not rules.allow_negative:
return _invalid("%s must not be negative" % label)
if len(fraction) > digits:
if digits == 0:
return _invalid("%s must be a whole number" % label)
return _invalid(
"%s must not have more than %d decimal %s" % (label, digits, "place" if digits == 1 else "places")
)
all_digits = (whole + fraction.ljust(digits, "0")).lstrip("0")
if len(all_digits) > len(MAX_SAFE) or (len(all_digits) == len(MAX_SAFE) and all_digits > MAX_SAFE):
return _invalid("%s is too large" % label)
canonical = ("-" if negative else "") + whole + ("" if fraction == "" else seps.decimal + fraction)
amount = parse_money(canonical, currency, style) # type: ignore[arg-type]
lo, hi = rules.min, rules.max
if lo is not None and hi is not None:
if amount.minor < lo or amount.minor > hi:
return _invalid(
"%s must be between %s and %s"
% (label, money_for_message(lo, currency, locale), money_for_message(hi, currency, locale))
)
elif lo is not None and amount.minor < lo:
return _invalid("%s must be %s or more" % (label, money_for_message(lo, currency, locale)))
elif hi is not None and amount.minor > hi:
return _invalid("%s must be %s or less" % (label, money_for_message(hi, currency, locale)))
return MoneyInputCheck(valid=True, amount=amount, message=None)format_money_input throws on bad input 13 tests
def format_money_input(amount: Money, locale: str) -> str
| amount | Money | the amount to put back into the input for editing |
| locale | string | how to group digits and mark the decimal |
| returns | string | digits with grouping and every decimal place, no symbol: "1,234.50", "1.234,50", "-0.05" |
For example
format_money_input(£1,234.50, en-GB)→ 1,234.50 pounds and penceformat_money_input(€1,234.50, de-DE)→ 1.234,50 Germanformat_money_input(€1,234.50, fr-FR)→ 1 234,50 French, with a plain space to type
from fune.form.money_input import format_money_input # form.money-input@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import List, NamedTuple
from .form_money_input_data import LOCALES ← this capability’s own data, compiled from data/locales.json into the same file by fune build
from .money_amount import Money ← from money.amount ^1.0.0 · built alongside by fune
from .money_currency_digits import currency_digits ← from money.currency-digits ^1.0.0 · built alongside by fune
class NumberSeparators(NamedTuple):
groups: List[str] # every group separator money.parse accepts in this style
group: str # the one this capability writes
decimal: str
def _lower(value: str) -> str:
return "".join(chr(ord(c) + 32) if "A" <= c <= "Z" else c for c in value)
def number_style_for(locale: str) -> str:
"""The money.parse number style for a locale: the exact locale first
(case-insensitive, "_" read as "-"), then its language alone."""
wanted = _lower(locale.replace("_", "-"))
for row in LOCALES:
if _lower(row.locale) == wanted:
return row.style
language = wanted.split("-")[0]
if language != "":
for row in LOCALES:
if _lower(row.locale) == language:
return row.style
raise ValueError(
'unknown locale "%s": give a BCP 47 locale this capability lists, such as en-GB, de-DE or fr-FR' % locale
)
def separators_of(style: str) -> NumberSeparators:
"""The separators of a money.parse number style, as money.parse reads them."""
if style == "comma-dot":
return NumberSeparators([","], ",", ".")
if style == "dot-comma":
return NumberSeparators(["."], ".", ",")
if style == "space-comma":
return NumberSeparators([" ", " ", " "], " ", ",")
if style == "apostrophe-dot":
return NumberSeparators(["'", "’"], "'", ".")
raise ValueError('unknown number style "%s"' % style)
def write_amount(minor: int, digits: int, style: str) -> str:
"""Minor units written with thousands grouping and every decimal place, no symbol."""
seps = separators_of(style)
raw = str(abs(minor)).rjust(digits + 1, "0")
whole = raw[: len(raw) - digits]
fraction = raw[len(raw) - digits :]
grouped = ""
for i, ch in enumerate(whole):
if i > 0 and (len(whole) - i) % 3 == 0:
grouped += seps.group
grouped += ch
number = grouped + seps.decimal + fraction if digits > 0 else grouped
return "-" + number if minor < 0 else number
def format_money_input(amount: Money, locale: str) -> str:
"""An amount as a money input shows it for editing: the locale's grouping
and decimal mark, every decimal place, no symbol."""
digits = currency_digits(amount.currency)
return write_amount(amount.minor, digits, number_style_for(locale))money_input_format throws on bad input 11 tests
def money_input_format(currency: str, locale: str) -> MoneyInputFormat
| currency | string | ISO 4217 code |
| locale | string | BCP 47 locale |
| returns | MoneyInputFormat | what a money input shows: the symbol, separators and an example |
For example
money_input_format(GBP, en-GB)→ currency GBP, symbol £, symbol before true, digits 2, group separator ,, decimal separator ., example 1,234.50 pounds in the UKmoney_input_format(EUR, de-DE)→ currency EUR, symbol €, symbol before true, digits 2, group separator ., decimal separator ,, example 1.234,50 euros in Germanymoney_input_format(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
from fune.form.money_input import money_input_format # form.money-input@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import NamedTuple
from .form_money_input_format_money_input import number_style_for, separators_of, write_amount ← formatMoneyInput, another function of this group · built into the same file, even by a slim install
from .form_money_input_types import MoneyInputFormat
from .money_currency_digits import currency_digits ← from money.currency-digits ^1.0.0 · built alongside by fune
from .money_format_data import CURRENCIES ← money.format’s rule data (^1.0.0) · built alongside by fune
from .money_parse_data import CURRENCY_SYMBOLS ← money.parse’s rule data (^1.0.0) · built alongside by fune
class CurrencySymbol(NamedTuple):
symbol: str
raw: str # money.format's own spacing ("CHF ", " kr"), for a sentence
before: bool
def currency_symbol(currency: str) -> CurrencySymbol:
"""money.format's symbol where it has the currency, else the first symbol
money.parse knows (before the amount), else the ISO code itself."""
for row in CURRENCIES:
if row.code == currency:
return CurrencySymbol(row.symbol.strip(" "), row.symbol, row.symbol_before)
for row in CURRENCY_SYMBOLS:
if row.code == currency:
return CurrencySymbol(row.symbol, row.symbol, True)
return CurrencySymbol(currency, currency + " ", True)
def money_for_message(minor: int, currency: str, locale: str) -> str:
"""An amount in a sentence: "£10.00", "100,00 kr", "€1.000,00"."""
digits = currency_digits(currency)
sym = currency_symbol(currency)
number = write_amount(abs(minor), digits, number_style_for(locale))
body = sym.raw + number if sym.before else number + sym.raw
return "-" + body if minor < 0 else body
def money_input_format(currency: str, locale: str) -> MoneyInputFormat:
"""What a money input for this currency and locale shows: the symbol,
the separators, and 1234.50 written the locale's way."""
digits = currency_digits(currency)
style = number_style_for(locale)
seps = separators_of(style)
sym = currency_symbol(currency)
example = int("1234" + "50".ljust(digits, "0")[:digits])
return MoneyInputFormat(
currency=currency,
symbol=sym.symbol,
symbol_before=sym.before,
digits=digits,
group_separator=seps.group,
decimal_separator=seps.decimal,
example=write_amount(example, digits, style),
)Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 4 dependencies, 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 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
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./form.money-input-1.0.0-python.fune, or fetch it from a terminal with fune pull form.money-input@1.0.0:python.
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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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`.