Price Formatting

Utilities for formatting prices and currencies

The price utilities help you format monetary values correctly.

Importing

import { price } from 'arky-sdk/utils';

Basic Formatting

Format Price

All Arky prices are in minor units (cents). Format for display:

// Format as USD
price.format(1999, 'USD');
// Returns: "$19.99"

price.format(1999, 'EUR');
// Returns: "€19.99"

price.format(1999, 'GBP');
// Returns: "£19.99"

// With locale
price.format(1999, 'EUR', 'de-DE');
// Returns: "19,99 €"

Format with Options

price.format(1999, 'USD', 'en-US', {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
});

Conversion

Cents to Dollars

price.toDollars(1999);
// Returns: 19.99

price.toDollars(2000);
// Returns: 20

Dollars to Cents

price.toCents(19.99);
// Returns: 1999

price.toCents(20);
// Returns: 2000

Calculations

Calculate Discount

const discount = price.calculateDiscount({
  originalPrice: 5000,  // $50.00
  discountType: 'PERCENTAGE',
  discountValue: 20     // 20%
});
// Returns: 1000 (= $10.00 discount)

const fixedDiscount = price.calculateDiscount({
  originalPrice: 5000,
  discountType: 'FIXED',
  discountValue: 1000   // $10.00 off
});
// Returns: 1000

Apply Discount

const finalPrice = price.applyDiscount(5000, {
  type: 'PERCENTAGE',
  value: 20
});
// Returns: 4000 (= $40.00)

Calculate Tax

const tax = price.calculateTax(5000, 0.08); // 8% tax
// Returns: 400 (= $4.00 tax)

const total = price.addTax(5000, 0.08);
// Returns: 5400 (= $54.00 total)

Sum Prices

const total = price.sum([1999, 2499, 999]);
// Returns: 5497

Comparison

Compare Prices

price.isEqual(1999, 1999);     // true
price.isGreater(2000, 1999);   // true
price.isLess(1999, 2000);      // true
price.isZero(0);               // true

Calculate Savings

const savings = price.calculateSavings(3999, 2999);
// Returns: { amount: 1000, percentage: 25 }

Display Helpers

Format Range

price.formatRange(1999, 4999, 'USD');
// Returns: "$19.99 - $49.99"

Format with Strikethrough

function PriceDisplay({ originalPrice, salePrice, currency }) {
  if (salePrice && salePrice < originalPrice) {
    return (
      <div className="price">
        <span className="original strikethrough">
          {price.format(originalPrice, currency)}
        </span>
        <span className="sale">
          {price.format(salePrice, currency)}
        </span>
        <span className="savings">
          Save {price.calculateSavings(originalPrice, salePrice).percentage}%
        </span>
      </div>
    );
  }

  return (
    <div className="price">
      {price.format(originalPrice, currency)}
    </div>
  );
}

Format Free

price.formatOrFree(0, 'USD');
// Returns: "Free"

price.formatOrFree(1999, 'USD');
// Returns: "$19.99"

Currency Info

Get Currency Symbol

price.getSymbol('USD'); // "$"
price.getSymbol('EUR'); // "€"
price.getSymbol('GBP'); // "£"
price.getSymbol('JPY'); // "¥"

Get Currency Info

price.getCurrencyInfo('USD');
// Returns: {
//   code: 'USD',
//   symbol: '$',
//   name: 'US Dollar',
//   decimals: 2
// }

Complete Example

function OrderSummary({ items, promoDiscount, taxRate, currency }) {
  const subtotal = price.sum(items.map(i => i.price * i.quantity));
  const discount = promoDiscount || 0;
  const taxableAmount = subtotal - discount;
  const tax = price.calculateTax(taxableAmount, taxRate);
  const total = taxableAmount + tax;

  return (
    <div className="order-summary">
      <div className="line">
        <span>Subtotal</span>
        <span>{price.format(subtotal, currency)}</span>
      </div>

      {discount > 0 && (
        <div className="line discount">
          <span>Discount</span>
          <span>-{price.format(discount, currency)}</span>
        </div>
      )}

      <div className="line">
        <span>Tax ({(taxRate * 100).toFixed(0)}%)</span>
        <span>{price.format(tax, currency)}</span>
      </div>

      <div className="line total">
        <span>Total</span>
        <span>{price.format(total, currency)}</span>
      </div>
    </div>
  );
}
Tip

Always store and transmit prices in cents (minor units) to avoid floating-point precision issues. Only format for display.