Currency Formatter

1.5.9 · deprecated · verified Sun Apr 19

The `currency-formatter` package is a JavaScript utility designed to format and unformat monetary values based on specified currency codes or locale settings. It is currently at version 1.5.9. The library's own maintainers strongly recommend against its use for most contemporary applications, advocating instead for the native JavaScript `Intl.NumberFormat` API, which offers superior and more up-to-date internationalization capabilities in modern browsers and Node.js environments. This library primarily serves as a compatibility layer for legacy contexts, such as very old browsers lacking `Intl` support, or specific Node.js setups that do not include `full-icu` data. Its release cadence is slow, and its active development appears to have ceased in favor of native solutions. It differentiates itself by providing a consistent API for environments where `Intl.NumberFormat` is unavailable or undesirable, internally leveraging the `accounting.js` library for its core formatting logic.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic currency formatting by code and locale, retrieving currency information, and unformatting monetary strings using the CommonJS API.

const currencyFormatter = require('currency-formatter');

// Format a number by currency code
let formattedUSD = currencyFormatter.format(1000000, { code: 'USD' });
console.log(`Formatted USD: ${formattedUSD}`); // Expected: $1,000,000.00

// Format a number by locale
let formattedEUR_de = currencyFormatter.format(1000000, { locale: 'de-DE' });
console.log(`Formatted EUR (de-DE): ${formattedEUR_de}`); // Expected: 1.000.000,00 €

// Get currency information
let usdInfo = currencyFormatter.findCurrency('USD');
console.log('USD Currency Info:', usdInfo);
/* Expected: {
 *   code: 'USD',
 *   symbol: '$',
 *   thousandsSeparator: ',',
 *   decimalSeparator: '.',
 *   symbolOnLeft: true,
 *   spaceBetweenAmountAndSymbol: false,
 *   decimalDigits: 2
 * } */

// Unformat a monetary string
let unformattedUSD = currencyFormatter.unformat('$1,000,000.00', { code: 'USD' });
console.log(`Unformatted USD: ${unformattedUSD}`); // Expected: 1000000

let unformattedEUR = currencyFormatter.unformat('1.000.000,00 €', { locale: 'de-DE' });
console.log(`Unformatted EUR: ${unformattedEUR}`); // Expected: 1000000.00

view raw JSON →