ChemPal Documentation - v1.6.0
    Preparing search index...

    Function formatUnitPriceExact

    • Formats a product's price per base unit at full precision — up to four decimal places, with no sub-cent collapse — for use as the hover tooltip behind the rounded formatUnitPrice display, so a value shown as "<$0.01/g" or "$0.07/g" reveals its actual "$0.0035/g" / "$0.072/g" on hover. Uses the same resolved amount as the display formatter (see resolveUnitPrice). Returns "" when there's no price or the quantity/unit can't be converted.

      Parameters

      • product: UnitPriceFields

        The product/variant price, usdPrice, currencyCode, quantity, and uom fields.

      • userSettings: undefined | PriceSettings

        The user's currency and currencyRate; defaults to USD at rate 1 when undefined.

      Returns string

      A localized full-precision "{price}/{unit}" string, or "" when unavailable.

      formatUnitPriceExact({ price: 5, usdPrice: 5, currencyCode: "USD", quantity: 1, uom: "kg" }, undefined);
      // => "$0.005/g"
      formatUnitPriceExact({ price: 40, usdPrice: 40, currencyCode: "USD", quantity: 500, uom: "g" }, { currency: "EUR", currencyRate: 0.9 });
      // => "€0.072/g"
      export function formatUnitPriceExact(
      product: UnitPriceFields,
      userSettings: PriceSettings | undefined,
      ): string {
      const resolved = resolveUnitPrice(product, userSettings);
      if (!resolved) return '';

      const { currency, perUnit, unitLabel } = resolved;
      return `${formatWithSymbol(currency, perUnit, unitPriceExactFractionDigits)}/${unitLabel}`;
      }