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

    Function formatUnitPrice

    • Formats a product's price per base unit for display, e.g. "$0.08/g" or "$19.99/pcs". Resolves the per-unit amount via resolveUnitPrice, then renders it at two decimal places, collapsing any positive unit price below one cent to "<$0.01/{unit}" (see formatPerUnitPrice) to keep the column narrow. The unrounded value is available via formatUnitPriceExact for a hover tooltip. 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 "{price}/{unit}" string, or "" when no unit price is available.

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

      return formatPerUnitPrice(resolved.currency, resolved.perUnit, resolved.unitLabel);
      }