Functional Weave
Code in TypeScript

fleet.cost-per-mile@1.0.0

README.md

1,195 bytes · view raw

# fleet.cost-per-mile

Whole-life cost per mile of a vehicle: what it cost over a period (depreciation,
fuel, maintenance, insurance and tax) divided by the miles it covered, in
tenths of a penny, which is how fleet reports quote it ("50.3p a mile").

## Shape

The costs come in as one `WholeLifeCosts` record of `Money`, all in one
currency; mixing currencies is an error. The result gives each part per mile,
the total per mile, and the total as text (`"50.3"`), all as integers in
tenths of a minor unit, rounded half-up (half a tenth away from zero).

## Rounding

Every figure is rounded on its own, and the total is rounded from the total
cost, not added up from the rounded parts. 6,040.00 over 12,000 miles is 50.3p
a mile; the rounded parts (25.0 + 15.0 + 5.0 + 3.8 + 1.6) add up to 50.4. A
report whose columns must add up should show the parts to more places or
footnote the difference.

## Edges

Depreciation can be negative (a vehicle sold for more than it cost), and the
per-mile figures are then negative too. Miles must be a whole number above
zero. This does not work out the costs themselves: see `fleet.fuel-cost`,
`fleet.vehicle-depreciation` and `fleet.ev-charging-cost`.