@telefonica/money-formatter
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

money-formatter

Simple multi-currency money formatter

Install

yarn add '@tuenti/money-formatter';

formatMoney({amount, precision, currency}, locale)

Returns a string always in UNIT_PRECISION

  • {number} money.amount indicates how much, depends on precision
  • {number} money.precision indicates amount precision, in base 10
  • {string} money.currency ISO 4217 currency representation
  • {string} locale=unknown_locale Locale

If currency is unknown, uses the given ISO 4217 description

moneyFormatter.formatMoney({
  amount: 2000,
  precision: 0,
  currency: 'EUR',
});

moneyFormatter.formatMoney(
  {
    amount: 2000,
    precision: 0,
    currency: 'EUR',
  },
  'es_ES'
);

moneyFormatter.formatMoney({
  amount: 2000,
  precision: 0,
  currency: 'POTATOES',
});

Output:

2,000.00 €
2.000,00 €
POTATOES 2,000.00

formatMoneyFittingPrecision({amount, precision, currency}, locale)

Returns a string in UNIT_PRECISION if the amount in UNIT_PRECISION is higher or equal than 1, in CENTI_PRECISION otherwise

  • {number} money.amount indicates how much, depends on precision
  • {number} money.precision indicates amount precision, in base 10
  • {string} money.currency ISO 4217 currency representation
  • {string} locale=unknown_locale Locale

If currency is unknown, uses the given ISO 4217 description

moneyFormatter.formatMoneyFittingPrecision({
  amount: 200,
  precision: 3,
  currency: 'EUR',
});

moneyFormatter.formatMoneyFittingPrecision(
  {
    amount: 200,
    precision: 0,
    currency: 'USD',
  },
  'en_US'
);

Output:

20,00 cénts.
$ 200.00

getFormatter(locale)

Workaround for returning both formatters, to avoid passing locale always as a parameter.

  • {string} locale=unknown_locale Locale
moneyFormatter.getFormatter('en_US').formatMoneyFittingPrecision({
  amount: 200,
  precision: 3,
  currency: 'EUR',
});

moneyFormatter.getFormatter().formatMoney(
  {
    amount: 200,
    precision: 0,
    currency: 'USD',
  },
  'en_US'
);

Readme

Keywords

none

Package Sidebar

Install

npm i @telefonica/money-formatter

Weekly Downloads

86

Version

1.2.1

License

UNLICENSED

Unpacked Size

87.8 kB

Total Files

19

Last publish

Collaborators

  • tdaf
  • aura
  • living-apps
  • lifecyle-novum