Carriers bill the higher of the actual weight and the dimensional weight (volume ÷ a divisor from your contract). The arithmetic is simple; the rounding is where bills and code both go wrong. Here is a small, testable version that keeps every quantity as an integer: millimetres, grams and BigInt.

// lengths in millimetres, weights in grams, all integers (BigInt) const ceilDiv = (a, b) => (a + b - 1n) / b; // for non-negative values const roundUpTo = (x, step) => ceilDiv(x, step) * step; function billableGrams({ sidesMm, actualG, divisorCm3PerKg, sideStepMm = 10n, weightStepG = 500n }) { // 1. round each side up (here: to the next whole centimetre) const sides = sidesMm.map(s => roundUpTo(s, sideStepMm)); // 2. volume in mm³; dimensional grams = volume_cm3 * 1000 / divisor = volume_mm3 / divisor const volMm3 = sides.reduce((a, s) => a * s, 1n); const dimG = ceilDiv(volMm3, divisorCm3PerKg); // never bill less than the exact value // 3. the higher of the two, then round up to the contract's weight step const higher = dimG > actualG ? dimG : actualG; return { sides, dimG, billedG: roundUpTo(higher, weightStepG) }; } console.log(billableGrams({ sidesMm: [400n, 300n, 300n], actualG: 4000n, divisorCm3PerKg: 5000n })); // { sides: [ 400n, 300n, 300n ], dimG: 7200n, billedG: 7500n } console.log(billableGrams({ sidesMm: [350n, 250n, 250n], actualG: 4000n, divisorCm3PerKg: 5000n })); // { sides: [ 350n, 250n, 250n ], dimG: 4375n, billedG: 4500n } console.log(billableGrams({ sidesMm: [342n, 245n, 251n], actualG: 4000n, divisorCm3PerKg: 5000n })); // sides rounded to 350, 250, 260 -> dimG 4550n, billedG 5000n 

A few choices worth copying:

  1. Integers only. Volume in mm³ divided by a divisor in cm³/kg lands directly in grams: 36,000,000 ÷ 5,000 = 7,200 g. No float ever touches the result.
  2. Round in the contract's order. Sides first, then the dimensional weight, then the billable weight to the contract's step. Change the order and the third example moves by half a kilo.
  3. Ceiling, never nearest. A carrier never rounds a parcel down, so a checker that rounds to nearest will report "overcharges" that are not there.

The test I find most useful is a property, not a value: making any side 1 mm larger can never make the billed weight smaller.

const base = { sidesMm: [342n, 245n, 251n], actualG: 4000n, divisorCm3PerKg: 5000n }; for (let i = 0; i < 3; i++) { const bigger = { ...base, sidesMm: base.sidesMm.map((s, j) => (j === i ? s + 1n : s)) }; console.assert(billableGrams(bigger).billedG >= billableGrams(base).billedG); } 

This is the step-by-step billable weight inside a paid tool we make, Shipping Bill Audit & Packaging Planner ($24, free demo): https://gettidycents.com/product/shipping-bill-audit-packaging-planner

Which rounding rule has surprised you most on a carrier contract?