Currency Formatter
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
-
TypeError: currencyFormatter.format is not a function
cause Attempting to use an ES module default import (e.g., `import currencyFormatter from 'currency-formatter'`) in an environment or bundler configuration where the `currency-formatter` package is treated as a CommonJS module without a compatible default export for ESM.fixUse the CommonJS `require` syntax: `const currencyFormatter = require('currency-formatter');`. -
My currency formatting is incorrect or doesn't match native browser behavior for certain locales/currencies.
cause Discrepancies between `currency-formatter`'s internal locale data or formatting logic and the more current/standardized behavior of the `Intl.NumberFormat` API, or incorrect `code`/`locale` options provided.fixFor modern applications, transition to `Intl.NumberFormat` for accurate and up-to-date internationalization. If `currency-formatter` must be used, ensure `code` and `locale` options are precisely configured and thoroughly test all target currencies/locales. -
My JavaScript bundle size increased significantly after adding currency-formatter.
cause The library is included in the production build even if its functionality is redundant because the target environments natively support `Intl.NumberFormat`.fixEvaluate your project's `Intl.NumberFormat` support requirements. If modern browser/Node.js support is sufficient, remove `currency-formatter` and use native APIs. For legacy support, consider conditional loading or a more granular polyfill.
Warnings
- breaking This library is explicitly deprecated by its own maintainers. The README strongly advises against its use for most modern applications, recommending the native `Intl.NumberFormat` API instead.
- gotcha Including `currency-formatter` may unnecessarily increase your application's bundle size if your target environments already support the native `Intl.NumberFormat` API.
- gotcha Formatting results from `currency-formatter` may differ from those produced by the native `Intl.NumberFormat` API due to different underlying implementations and locale data, potentially leading to inconsistent UI.
- gotcha The `unformat` function's behavior relies heavily on the provided `code` or `locale` parameters to correctly parse monetary strings, and may produce unexpected results if these do not match the input string's format.
Install
-
npm install currency-formatter -
yarn add currency-formatter -
pnpm add currency-formatter
Imports
- currencyFormatter
import currencyFormatter from 'currency-formatter';
const currencyFormatter = require('currency-formatter'); - format
import { format } from 'currency-formatter';const { format } = require('currency-formatter'); // Or currencyFormatter.format(...) - findCurrency
import { findCurrency } from 'currency-formatter';const { findCurrency } = require('currency-formatter'); // Or currencyFormatter.findCurrency(...)
Quickstart
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