Functional Weave
Code in Python

Suites · money-basics

Money basics

Amounts in integer minor units: add, total, apply rates, split without losing a penny, compare, format, parse and convert.

16 capabilities (8 core, 8 optional) and 2 gaps, in build order. Put each line in the file that calls it, then fune build.

1. Amounts

Hold money as currency-tagged integer minor units and do plain arithmetic on it safely.

money.amount core
The Money type ({ minor, currency }) every other money capability takes and returns.
A currency-tagged monetary amount held in integer minor units.
from fune.money.amount import money  # money.amount@^1
money.add core
Adds and subtracts amounts, refusing to mix currencies by accident.
Add, subtract and negate monetary amounts, refusing to mix currencies.
from fune.money.add import add_money  # money.add@^1
money.sum core
Totals a list of amounts, with an explicit currency for an empty list.
Total a list of monetary amounts, with an explicit currency for the empty case.
from fune.money.sum import sum_money  # money.sum@^1
money.compare core
Compares, mins, maxes and clamps amounts of one currency without unwrapping them.
Compare two amounts of one currency, refusing to compare across currencies; min, max and clamp too.
from fune.money.compare import compare_money, min_money, max_money, clamp_money  # money.compare@^2

2. Rates and rounding

Apply percentages and divide amounts with the rounding stated, never a float.

money.apply-rate core
Applies a basis-point rate (VAT, discount, commission) to an amount with explicit rounding.
Apply a rate expressed in basis points to a monetary amount, with an explicit rounding mode.
from fune.money.apply_rate import apply_rate  # money.apply-rate@^1
math.round-div core
Integer division with a named rounding mode, for any money maths the others do not cover.
Integer division with an explicit rounding mode, for money arithmetic that must not drift.
from fune.math.round_div import round_div  # math.round-div@^1
math.basis-points optional
Converts "12.5%" to and from basis points exactly, for rates typed in by users.
Convert a rate between percent, basis points and a plain ratio exactly, as decimal text or integer basis points.
from fune.math.basis_points import convert_rate, to_basis_points, from_basis_points  # math.basis-points@^2
math.percent-change optional
Percentage change between two figures in basis points, for "up 4.2% on last month".
Percentage change from one value to another, in basis points, with an explicit rounding mode.
from fune.math.percent_change import percent_change  # math.percent-change@^1
math.rational optional
Exact fractions for rates that must not drift across many steps.
Exact fraction arithmetic, always reduced, for rates and ratios that must not drift.
from fune.math.rational import calculate_rational, rational, add_rational, subtract_rational, multiply_rational, divide_rational, compare_rational, rational_to_integer  # math.rational@^2
math.round-div-big optional
Rounded division on numbers past 2^53, for very large totals.
Divide two integers of any size with an explicit rounding mode, on decimal strings, for exact money sums past 2^53.
from fune.math.round_div_big import round_div_big  # math.round-div-big@^1

3. Splitting

Divide an amount between people, periods or lines so the parts add back to the whole.

money.allocate core
Splits an amount by ratios (60/40, by quantity) without losing or inventing a penny.
Split an amount across ratios without losing or inventing a single minor unit.
from fune.money.allocate import allocate  # money.allocate@^1
money.split-even optional
Splits an amount into n near-equal parts that sum exactly, for instalments or shares.
Split an amount into n near-equal parts that add back up to exactly the whole.
from fune.money.split_even import split_even  # money.split-even@^1

4. Display and input

Show amounts to people and read what they type.

money.format core
Renders an amount as text with the currency's real number of decimal places.
Render a monetary amount as text, using the currency's real minor-unit precision.
from fune.money.format import format_money  # money.format@^1
money.parse optional
Reads "£1,234.50" or "1.234,50 €" typed by a user into minor units, strictly.
Parse "£1,234.50" or "1.234,50 €" into integer minor units, strictly: no guessing, no rounding.
from fune.money.parse import parse_money  # money.parse@^1
money.currency-digits optional
How many decimal places each ISO 4217 currency has, for input boxes and validation.
How many decimal places a currency's minor unit has, from the ISO 4217 list.
from fune.money.currency_digits import currency_digits  # money.currency-digits@^1

5. Other currencies

Convert between currencies at a rate you hold.

money.convert optional
Converts an amount at a supplied exact rate with explicit rounding.
Convert an amount to another currency at a supplied exact rate, with an explicit rounding mode.
from fune.money.convert import convert_money  # money.convert@^1

Gaps

What this kind of app usually needs that Functional Weave does not have yet: write these yourself, or use a service.

  • fx-rate-source Where exchange rates come from: money.convert takes a rate you supply; fetching and storing daily rates (ECB, a provider's API) is yours.
  • money-storage Storing amounts: the database columns (integer minor units plus a currency code) and their migrations are yours.