retail.volumetric-weight
Chargeable parcel weight: the greater of actual and volumetric (dimensional) weight, rounded up to a billing step.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 16 tests, run in TypeScript, Python and Rust.
What it does
Carriers charge for the space a parcel takes as well as its weight. The volumetric weight is `length x width x height / divisor`, where the divisor is in cubic centimetres per kilogram (5000 for most express couriers, 4000 or 6000 for some services), and the chargeable weight is whichever of actual and volumetric is greater.
Working in millimetres and grams keeps it all integers: mm³ / (cm³/kg) is exactly grams, because 1 cm³ = 1000 mm³ and 1 kg = 1000 g cancel. A 400 x 300 x 200 mm box at divisor 5000 is 24,000,000 / 5000 = 4800 g.
For example
chargeableWeight(400, 300, 200, 2,500, 5,000, 500)→ actual grams 2,500, volumetric grams 4,800, chargeable grams 5,000, basis volumetric a light bulky box is charged on volume, rounded up to the half kilochargeableWeight(400, 300, 200, 6,200, 5,000, 500)→ actual grams 6,200, volumetric grams 4,800, chargeable grams 6,500, basis actual a heavy small box is charged on actual weightchargeableWeight(400, 300, 200, 4,800, 5,000, 500)→ actual grams 4,800, volumetric grams 4,800, chargeable grams 5,000, basis actual equal weights: the basis is actual
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 chargeableWeight(lengthMm: number, widthMm: number, heightMm: number, actualGrams: number, divisor: number, roundUpToGrams: number): ChargeableWeight
| lengthMm | int | outside dimensions in millimetres, 1 or more |
| widthMm | int | |
| heightMm | int | |
| actualGrams | int | weight on the scales, 0 or more |
| divisor | int | the carrier's volumetric divisor in cm3 per kg: 5000 is common, 4000 and 6000 also |
| roundUpToGrams | int | the carrier's billing step: 1, 500 for half kilos, 1000 for whole kilos |
| returns | ChargeableWeight |
The types it declares, generated into your project
/** Both weights, which one won, and the weight to charge on. */
export interface ChargeableWeight {
readonly actualGrams: number;
/** length x width x height / divisor, rounded up to the gram */
readonly volumetricGrams: number;
/** the greater of the two, rounded up to the billing step */
readonly chargeableGrams: number;
/** volumetric only when it is strictly greater */
readonly basis: WeightBasis;
}
export type WeightBasis = "actual" | "volumetric";
Your code names it in one line, in the file that uses it
import { chargeableWeight } from "#fune/retail.volumetric-weight@^1";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { roundDiv } from "./math_round_div.ts"; ← from math.round-div ^1.0.0 · built alongside by fune
import { type ChargeableWeight } from "./retail_volumetric_weight_types.ts";
/**
* The weight a carrier charges on: the greater of actual and volumetric,
* rounded up to the carrier's billing step.
*
* mm³ / (cm³ per kg) is exactly grams, so everything stays in integers.
*/
export function chargeableWeight(
lengthMm: number,
widthMm: number,
heightMm: number,
actualGrams: number,
divisor: number,
roundUpToGrams: number,
): ChargeableWeight {
for (const d of [lengthMm, widthMm, heightMm]) {
if (!Number.isInteger(d) || d < 1) throw new RangeError(`dimensions must be 1 mm or more, received ${d}`);
}
if (!Number.isInteger(actualGrams) || actualGrams < 0) {
throw new RangeError(`actualGrams must not be negative, received ${actualGrams}`);
}
if (!Number.isInteger(divisor) || divisor < 1) throw new RangeError(`divisor must be 1 or more, received ${divisor}`);
if (!Number.isInteger(roundUpToGrams) || roundUpToGrams < 1) {
throw new RangeError(`roundUpToGrams must be 1 or more, received ${roundUpToGrams}`);
}
const volumetricGrams = roundDiv(lengthMm * widthMm * heightMm, divisor, "up");
const heavier = Math.max(actualGrams, volumetricGrams);
return {
actualGrams,
volumetricGrams,
chargeableGrams: roundDiv(heavier, roundUpToGrams, "up") * roundUpToGrams,
basis: volumetricGrams > actualGrams ? "volumetric" : "actual",
};
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 1 dependency, 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 retail.volumetric-weight
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./retail.volumetric-weight-1.0.0-typescript.fune, or fetch it from a terminal with fune pull retail.volumetric-weight@1.0.0:typescript.
The whole function, every language, is one file too: retail.volumetric-weight-1.0.0.fune, 12,793 bytes, sha256 aae507e675957ea20e4a1fa476747be9fc726667d41008986e4e2dedfe705a4d. 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 retail.volumetric-weight
after — your function gets the result and the arguments, and returns the final result.
// fune: after retail.volumetric-weight
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 retail.volumetric-weight
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 retail.volumetric-weight --steps.
// fune: step retail.volumetric-weight 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 | |
|---|---|---|---|
| a light bulky box is charged on volume, rounded up to the half kilo | 400, 300, 200, 2,500, 5,000, 500 | → | actual grams 2,500, volumetric grams 4,800, chargeable grams 5,000, basis volumetric |
| a heavy small box is charged on actual weight | 400, 300, 200, 6,200, 5,000, 500 | → | actual grams 6,200, volumetric grams 4,800, chargeable grams 6,500, basis actual |
| equal weights: the basis is actual | 400, 300, 200, 4,800, 5,000, 500 | → | actual grams 4,800, volumetric grams 4,800, chargeable grams 5,000, basis actual |
| divisor 6000 makes the same box lighter | 400, 300, 200, 2,500, 6,000, 500 | → | actual grams 2,500, volumetric grams 4,000, chargeable grams 4,000, basis volumetric |
| divisor 4000 makes it heavier | 400, 300, 200, 2,500, 4,000, 500 | → | actual grams 2,500, volumetric grams 6,000, chargeable grams 6,000, basis volumetric |
| volumetric grams round up, where floor division gives 7385 | 333, 333, 333, 1,000, 5,000, 1 | → | actual grams 1,000, volumetric grams 7,386, chargeable grams 7,386, basis volumetric |
| billing in whole kilos rounds 7386 g up to 8 kg | 333, 333, 333, 1,000, 5,000, 1,000 | → | actual grams 1,000, volumetric grams 7,386, chargeable grams 8,000, basis volumetric |
| a tiny parcel still has at least 1 g of volume | 10, 10, 10, 100, 5,000, 100 | → | actual grams 100, volumetric grams 1, chargeable grams 100, basis actual |
| a weightless parcel is charged on volume, then the minimum step | 100, 100, 100, 0, 5,000, 500 | → | actual grams 0, volumetric grams 200, chargeable grams 500, basis volumetric |
| dimension order does not matter | 200, 400, 300, 2,500, 5,000, 500 | → | actual grams 2,500, volumetric grams 4,800, chargeable grams 5,000, basis volumetric |
Show the other 6 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| an exact billing step is not rounded further | 500, 400, 250, 1,000, 5,000, 500 | → | actual grams 1,000, volumetric grams 10,000, chargeable grams 10,000, basis volumetric |
| a pallet-sized parcel stays exact in integers | 1,200, 1,000, 1,500, 150,000, 5,000, 1,000 | → | actual grams 150,000, volumetric grams 360,000, chargeable grams 360,000, basis volumetric |
| a zero dimension is an error | 0, 300, 200, 2,500, 5,000, 500 | → | error: dimensions must be 1 mm or more |
| a negative weight is an error | 400, 300, 200, -1, 5,000, 500 | → | error: actualGrams must not be negative |
| a zero divisor is an error | 400, 300, 200, 2,500, 0, 500 | → | error: divisor must be 1 or more |
| a zero billing step is an error | 400, 300, 200, 2,500, 5,000, 0 | → | error: roundUpToGrams must be 1 or more |
More from the author
## Rounding
Two roundings, both up, both documented because both move money:
1. The volumetric weight is rounded up to the whole gram (7385.2 g is 7386 g). Carriers never round a parcel's size down. 2. The chargeable weight is rounded up to `roundUpToGrams`, the carrier's billing step (most bill in 500 g or 1 kg steps). Pass 1 to leave it at the gram.
When the two weights are equal the basis is `actual`.
## Inputs
Measure the longest, widest and tallest points of the packed parcel, bulges included; that is what the carrier's scanner will do. The order of the three dimensions does not matter. Carriers that measure in centimetres and round each dimension first need that rounding done before calling this.
Files
| Path | Bytes |
|---|---|
| README.md | 1,299 |
| impl/python.py | 1,593 |
| impl/rust.rs | 2,080 |
| impl/typescript.ts | 1,453 |
| vectors.json | 3,036 |