# Cost per unit calculator

Compare two package sizes at different prices by the cost of a single unit.

/**
 * Calculator maths for currentcosts.com.
 *
 * Plain functions on `window.CRCALC`. This file is the single source of the maths: it is
 * served to the browser as the calculator bundle and it is the file the Node test
 * (scripts/test-tool-math.mjs) imports. The tests therefore exercise exactly the code users run.
 *
 * Every function validates its inputs and returns NaN rather than throwing, so a bad input
 * produces an error message instead of a broken page.
 */
(function (root) {
  "use strict";

  function isNum(v) { return typeof v === "number" && Number.isFinite(v); }

  /** Standard amortising payment. i = monthly rate, n = months. */
  function monthlyPayment(principal, annualRatePct, months) {
    if (!isNum(principal) || !isNum(annualRatePct) || !isNum(months)) return NaN;
    if (principal  balance) princ = balance;
      if (princ  1e-9) cheaper = a  0
        ? Math.abs(a - b) / Math.max(a, b) * 100 : 0 };
  }

  /** Months to clear a balance at a fixed payment, plus the effect of paying more. */
  function payoffMonths(balance, annualRatePct, payment) {
    if (!isNum(balance) || !isNum(annualRatePct) || !isNum(payment)) return null;
    if (balance

      Option A price (USD)

      Option A quantity

      Option B price (USD)

      Option B quantity

      Unit name

  Compare unit cost

Unit cost is price divided by quantity. It is the only honest way to compare
different package sizes, and it is the number printed in small type on the shelf label in most
jurisdictions for exactly that reason. The comparison assumes the two options are otherwise
identical in quality and that you will actually use the larger quantity.

(function () {
  var C = window.CRCALC;
  function $(id) { return document.getElementById(id); }
  function num(id) { var el = $(id); if (!el) return NaN; var v = el.value === "" ? NaN : Number(el.value); return Number.isFinite(v) ? v : NaN; }
  function money(n, d) { return "$" + n.toLocaleString("en-US", { minimumFractionDigits: d === undefined ? 2 : d, maximumFractionDigits: d === undefined ? 2 : d }); }
  function render() {
    var pa = num("cu-price-a"), qa = num("cu-qty-a"), pb = num("cu-price-b"), qb = num("cu-qty-b");
    var unit = ($("cu-unit").value || "unit").replace(/[&"]/g, "");
    var err = $("cu-err"), out = $("cu-out");
    err.hidden = true;
    if (![pa, qa, pb, qb].every(Number.isFinite)) { err.hidden = false; err.textContent = "Enter a price and a quantity for both options."; out.innerHTML = ""; return; }
    if (qa " + money(r.unitA) + " vs " + money(r.unitB) + " per " + unit + "

" +
      "" + verdict + " On a like-for-like basis, if you were buying " + qb + " " + unit + " at option A's rate it would cost " + money(r.unitA * qb) + " instead of " + money(pb) + ".

" +
      "Unit costOptionPriceQuantityPer " + unit + "

" +
      "Option A" + money(pa) + "" + qa + "" + money(r.unitA, 4) + "

" +
      "Option B" + money(pb) + "" + qb + "" + money(r.unitB, 4) + "

" +
      "

";
  }
  window.__crRender = render;
  $("cu-go").addEventListener("click", render);
  ["cu-price-a", "cu-qty-a", "cu-price-b", "cu-qty-b", "cu-unit"].forEach(function (id) { $(id).addEventListener("input", render); });
  render();
})();

Source: https://currentcosts.com/tools/cost-per-unit-calculator/
