tariff

usage-billing rating — a clickable playground

tariff turns a usage quantity into money. A per-unit plan charges one rate for every unit; a tiered schedule splits the quantity into bands — graduated charges each band at its own rate and sums them, while volume charges the whole quantity at the single rate of the band it lands in; a package plan rounds up to whole blocks. Rates are exact rationals, amounts are integer minor units (cents), and rounding is always the caller's explicit choice.

Everything below runs in your tab over the pure library compiled to WebAssembly — no backend, no network. Rates cross in as exact strings ("0.0006" or "6/10000"); amounts come back as int64 minor units plus a formatted string. Reload to reset. Built from zkrebbekx/tariff.

Start with a story:

Graduated vs volume — the same schedule, two totals

Set a tiered price schedule and a quantity, and see both interpretations at once. Graduated charges each tier's units at that tier's rate and sums them. Volume charges the whole quantity at the single rate of the tier it lands in — so as usage grows into a cheaper tier, the volume total can fall. Tiers are half-open: tier i covers (prevUpTo, upTo].

Price schedule (rate per unit)

Quantity6
Graduated
Volume

Each graduated segment is one tier's units at its own rate; the volume bar is the whole quantity at one landed rate.

Total vs quantity across the whole range. Watch the volume line step down at a tier crossing under the steep-drop schedule.

Order matters — drag to reorder the invoice

Charges, discounts, minimums and credits combine in your order, because that order is where real systems disagree. Drag the steps to reorder them and watch the running total after each step, and the final invoice, change. The classic case: [charge $100, 10% off, min $95] lands at $95, but moving the minimum above the discount gives $90. The invoice lines always reconcile to the total.

Invoice

LineQtyRateAmount

Package & stairstep

Package rounds the chargeable quantity up to whole blocks after the free allowance: blocks = ceil((qty − free) / N). Stairstep charges one flat fee for landing in a tier.

LineQtyRateAmount

Proration — a mid-cycle change

A plan priced per period changes partway through. tariff credits the unused old price, charges the new price for the time left, and nets them — the verified cross-vendor model. Drag the change marker; flip the basis between real seconds and whole days.

Billing period: January 2024 · change on Jan 16, 12:00
Credit (unused old)
Charge (remaining new)
Net billed

Month-end cycle boundaries

An anniversary cycle anchored on Jan 31 clamps to each month's last valid day and back — no drift to the 28th.

Loading the tariff engine…