Back to Arky

Price Formatting

Utilities for currency-safe minor-unit conversion and formatting

Arky stores money as integer minor units of its currency. The scale depends on the currency: two decimal places for most, none for JPY, KRW, CLP or VND, three for KWD, BHD, JOD, OMR or TND. Always carry the currency with the amount instead of assuming cents.

Importing

import {
	convertToMajor,
	convertToMinor,
	formatMinor,
	formatMoney,
	formatPrice,
	getCurrencyMinorUnits,
	getCurrencyName,
	getCurrencySymbol,
	SUPPORTED_STORE_CURRENCIES,
} from "arky-sdk/utils";

Format Minor Units

Pass the integer amount, its currency and the language to format in:

formatMinor(1999, "usd", "en");
formatMinor(1999, "eur", "de");
formatMinor(1999, "jpy", "ja");
formatMoney({ amount: 4500, currency: "bam" }, "bs");
formatPrice(product.price, "en");

There is no default language: the formatter takes the one you name, as every Arky text does.

Convert Between Major And Minor Units

convertToMajor(1999, "usd");
convertToMinor(19.99, "usd");
convertToMinor(1999, "jpy");

Never divide or multiply by 100 yourself; that is wrong for zero- and three-decimal currencies.

Currency Metadata

getCurrencyMinorUnits("usd");
getCurrencySymbol("usd", "en");
getCurrencyName("usd", "en");

console.log(SUPPORTED_STORE_CURRENCIES);

Display An API Amount

API money already carries its currency:

function OrderTotal({ order }: { order: Order }) {
	return <span>{formatMinor(order.totals.total, order.currency, order.language)}</span>;
}
Tip

Store and send integer minor-unit amounts together with their currency. Convert only at input and display.

Warning

Unsupported currency codes are refused instead of guessing a decimal scale.