finance.invoice.line-total
Net total for one invoice line: unit price by quantity, less a discount.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 9 tests, run in TypeScript, Python and Rust.
What it does
The discount is rounded once, at the line, before tax is applied. Rounding the discount and the tax in the other order produces answers that differ by a penny on roughly one line in five, which is exactly the kind of difference that turns into a reconciliation project.
For example
lineTotal(£1.99, 3, 0%)→ £5.97 three units at 1.99lineTotal(£1.99, 3, 10%)→ £5.37 ten percent off three unitslineTotal(£500.00, 1, 0%)→ £500.00 single unit, no discount
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 lineTotal(unitPrice: Money, quantity: number, discountBasisPoints: number): Money
| unitPrice | Money | |
| quantity | int | negative for credit lines |
| discountBasisPoints | int | 1000 = 10% off |
| returns | Money |
Your code names it in one line, in the file that uses it
import { lineTotal } from "#fune/finance.invoice.line-total@^1";
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, money } from "./money_amount.ts"; ← from money.amount ^1.0.0 · built alongside by fune
import { applyRate } from "./money_apply_rate.ts"; ← from money.apply-rate ^1.0.0 · built alongside by fune
/**
* Net total for one invoice line.
*
* The discount is rounded once, here, before any tax is applied. Rounding the
* discount and the tax in the other order produces answers that differ by a
* penny on roughly one line in five.
*/
export function lineTotal(unitPrice: Money, quantity: number, discountBasisPoints: number = 0): Money {
if (!Number.isInteger(quantity)) {
throw new TypeError(`quantity must be an integer, received ${quantity}`);
}
if (!Number.isInteger(discountBasisPoints) || discountBasisPoints < 0 || discountBasisPoints > 10000) {
throw new RangeError(`discountBasisPoints must be between 0 and 10000, received ${discountBasisPoints}`);
}
const gross = money(unitPrice.minor * quantity, unitPrice.currency);
const discount = applyRate(gross, discountBasisPoints, "half-up");
return money(gross.minor - discount.minor, gross.currency);
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 2 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 finance.invoice.line-total
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./finance.invoice.line-total-1.0.0-typescript.fune, or fetch it from a terminal with fune pull finance.invoice.line-total@1.0.0:typescript.
The whole function, every language, is one file too: finance.invoice.line-total-1.0.0.fune, 7,070 bytes, sha256 c2462dccc8a0b7672e97f8e360047906aee3004d804143ee03dd6a0afcc9520a. 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 finance.invoice.line-total
after — your function gets the result and the arguments, and returns the final result.
// fune: after finance.invoice.line-total
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 finance.invoice.line-total
// fune: replace money.apply-rate in finance.invoice.line-total
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 finance.invoice.line-total --steps.
// fune: step finance.invoice.line-total 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.
| Case | Arguments | Expected | |
|---|---|---|---|
| three units at 1.99 | £1.99, 3, 0% | → | £5.97 |
| ten percent off three units | £1.99, 3, 10% | → | £5.37 |
| single unit, no discount | £500.00, 1, 0% | → | £500.00 |
| zero quantity is zero | £500.00, 0, 0% | → | £0.00 |
| a credit line is negative | £500.00, -1, 0% | → | -£500.00 |
| a discount that lands on half a penny rounds up | £99.99, 1, 10% | → | £89.99 |
| a full discount leaves nothing | £19.99, 2, 100% | → | £0.00 |
| a fractional quantity is an error | £1.99, 2.5, 0% | → | error: quantity must be an integer |
| a discount above 100 percent is an error | £1.99, 1, 100.01% | → | error: between 0 and 10000 |
Files
| Path | Bytes |
|---|---|
| README.md | 300 |
| impl/python.py | 1,076 |
| impl/rust.rs | 1,620 |
| impl/typescript.ts | 995 |
| vectors.json | 1,348 |