money.apply-rate
Apply a rate expressed in basis points to a monetary amount, with an explicit rounding mode.
1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra
Pinned by 10 tests, run in TypeScript, Python and Rust · fewer than the registry now requires. 1.0.1 adds them.
What it does
Rates are integers in basis points, never floats. A VAT rate of 20% is 2000, not 0.2, so the calculation stays in exact integer arithmetic from input to rounded result.
This single function covers VAT, discounts, interest, commission and service charges: they differ in the rate and the rounding policy, not in the arithmetic.
For example
apply_rate(£100.00, 20%, half-up)→ £20.00 20 percent VAT on 100 poundsapply_rate(£1.99, 20%, half-up)→ £0.40 20 percent of 1.99 rounds upapply_rate(£49.99, 5%, half-up)→ £2.50 5 percent reduced rate
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 apply_rate(amount: Money, basis_points: int, mode: RoundingMode) -> Money
| amount | Money | |
| basis_points | int | 10000 = 100%, 2000 = 20%, 25 = 0.25% |
| mode | RoundingMode | |
| returns | Money |
Your code names it in one line, in the file that uses it
from fune.money.apply_rate import apply_rate # money.apply-rate@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from .math_round_div import RoundingMode, round_div ← from math.round-div ^1.0.0 · built alongside by fune
from .money_amount import Money, money ← from money.amount ^1.0.0 · built alongside by fune
def apply_rate(amount: Money, basis_points: int, mode: RoundingMode = "half-up") -> Money:
"""Apply a rate given in basis points (10000 = 100%).
Rates are integers, never floats, so VAT, discounts, interest and
commission all stay in exact arithmetic right up to the single rounding
step the caller asked for.
"""
if isinstance(basis_points, bool) or not isinstance(basis_points, int):
raise TypeError("basis_points must be an integer, received %r" % (basis_points,))
return money(round_div(amount.minor * basis_points, 10000, mode), amount.currency)
def apply_rate_complement(amount: Money, basis_points: int, mode: RoundingMode = "half-up") -> Money:
"""The complement: what is left after the rate is taken off."""
return money(amount.minor - apply_rate(amount, basis_points, mode).minor, amount.currency)Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 2 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 money.apply-rate
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./money.apply-rate-1.0.0-python.fune, or fetch it from a terminal with fune pull money.apply-rate@1.0.0:python.
The whole function, every language, is one file too: money.apply-rate-1.0.0.fune, 6,558 bytes, sha256 c626a99e25b85e9edc9b6c0c560d80199b6d5594d0d01a9a982a6005a46e20c9. 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.apply-rate
after — your function gets the result and the arguments, and returns the final result.
# fune: after money.apply-rate
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 math.round-div in money.apply-rate
# fune: replace money.amount in money.apply-rate
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.apply-rate --steps.
# fune: step money.apply-rate 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.
This version has fewer tests than the registry now requires. It was published before every function had to have 8. 1.0.1 meets it, and a project on ^1.0.0 installs that or newer.
- applyRate throws (throws yes) but none of its 10 tests expects an error; add an "expectError" vector for each error it documents, or declare `throws no` if it cannot throw
| Case | Arguments | Expected | |
|---|---|---|---|
| 20 percent VAT on 100 pounds | £100.00, 20%, half-up | → | £20.00 |
| 20 percent of 1.99 rounds up | £1.99, 20%, half-up | → | £0.40 |
| 5 percent reduced rate | £49.99, 5%, half-up | → | £2.50 |
| exact half rounds up under half-up | £0.05, 50%, half-up | → | £0.03 |
| exact half rounds to even under half-even | £0.05, 50%, half-even | → | £0.02 |
| down never rounds up | £1.99, 20%, down | → | £0.39 |
| zero rate | £123.45, 0%, half-up | → | £0.00 |
| 100 percent is the amount itself | £123.45, 100%, half-up | → | £123.45 |
| quarter of a percent | £1,000.00, 0.25%, half-up | → | £2.50 |
| credits rate symmetrically | -£1.99, 20%, half-up | → | -£0.40 |
Files
| Path | Bytes |
|---|---|
| README.md | 348 |
| impl/python.py | 948 |
| impl/rust.rs | 1,001 |
| impl/typescript.ts | 952 |
| vectors.json | 1,549 |