Click here for v1.x documentation.
Dinero.js
Dinero.js version

Formatting non-decimal currencies

The great majority of circulating currencies are decimal. If you're working with those, the Dinero.js formatting utility should cover most of your use cases.

However, you might also work with non-decimal currencies. Typical use cases are ancient currencies such as the ancient Greek drachma, or fictional currencies like the wizarding currencies in the Harry Potter universe. If you're building a numismatic site or a game with its own currency, you might have advanced formatting needs.

Copy linkHandling currencies with a single subdivision

Out of the box, you can format any non-decimal Dinero object using toUnits.

import { dinero, toUnits } from 'dinero.js';
import pluralize from 'pluralize';

const labels = ['drachma', 'obol'];

function transformer({ value, currency }) {
  return value
    .filter((amount) => amount > 0)
    .map((amount, index) => `${amount} ${pluralize(labels[index], amount)}`)
    .join(', ');
}

const d = dinero({
  amount: 9,
  currency: {
    code: 'GRD',
    base: 6,
    exponent: 1,
  },
});

toUnits(d, transformer); // "1 drachma, 3 obols"

Copy linkHandling currencies with multiple subdivisions

While most circulating currencies have a single minor currency unit, many ancient currencies have multiple subdivisions. That's the case for most pre-decimalization European currencies such as the livre tournois in the French Old Regime or the pound sterling in Great Britain before 1971. That's also the case of some fictional currencies.

When working with such currencies, you can specify each subdivision with an array.

For example, let's say you're building a Candy Crush clone where users can buy bonuses with an in-game currency: donuts, cookies, and lollipops. In your game, a donut equals 30 cookies, and a cookie equals 16 lollipops. If a bonus costs 720 lollipops, you might want to format it as "1 donut and 15 cookies".

import { dinero, toUnits } from 'dinero.js';

const POP = {
  code: 'POP',
  base: [30, 16],
  exponent: 1,
};

const labels = ['🍩', '🍪', '🍭'];

function transformer({ value }) {
  return value
    .filter((amount) => amount > 0)
    .map((amount, index) => `${amount} ${labels[index]}`)
    .join(' and ');
}

const d = dinero({ amount: 720, currency: POP });

toUnits(d, transformer); // "1 🍩 and 15 🍪"