currency-formatter vs currency.js vs dinero.js vs numeral
JavaScript Currency Formatting Libraries
currency-formattercurrency.jsdinero.jsnumeralSimilar Packages:

JavaScript Currency Formatting Libraries

These libraries are designed to handle currency formatting and manipulation in JavaScript applications. They provide developers with tools to format numbers as currency, perform arithmetic operations on currency values, and ensure proper localization based on different currencies and locales. Each library has its unique features and use cases, making them suitable for various scenarios in web development.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
currency-formatter0213-154 years agoMIT
currency.js03,365-585 years agoMIT
dinero.js06,718858 kB413 days agoMIT
numeral09,724-3499 years agoMIT

Feature Comparison: currency-formatter vs currency.js vs dinero.js vs numeral

Currency Formatting

  • currency-formatter:

    currency-formatter provides simple methods to format numbers as currency strings based on locale and currency type. It supports various currencies and allows customization of decimal and thousand separators, making it easy to display currency values correctly.

  • currency.js:

    currency.js allows you to format currency values easily, but its primary focus is on arithmetic operations. It provides methods to format numbers as currency strings, ensuring that the output adheres to the specified currency format.

  • dinero.js:

    dinero.js offers comprehensive currency formatting capabilities, allowing you to format money objects into strings with specific currency symbols and formats. It supports localization and can handle various currencies seamlessly, making it ideal for international applications.

  • numeral:

    numeral provides a flexible formatting system that can format numbers as currency, percentages, and more. It allows for extensive customization of the output format, making it suitable for applications that require diverse number formatting.

Arithmetic Operations

  • currency-formatter:

    currency-formatter does not support arithmetic operations; it is strictly for formatting purposes. If calculations are needed, additional logic must be implemented separately.

  • currency.js:

    currency.js excels in performing accurate arithmetic operations on currency values. It ensures that calculations maintain precision, making it suitable for applications that require financial computations without rounding errors.

  • dinero.js:

    dinero.js is designed for complex arithmetic operations, allowing you to perform addition, subtraction, multiplication, and division on money objects. It maintains immutability, ensuring that operations do not alter the original value, which is crucial for financial applications.

  • numeral:

    numeral does not focus on arithmetic operations but can format numbers after calculations are performed. For arithmetic operations, you would need to use standard JavaScript methods or combine it with other libraries.

Localization Support

  • currency-formatter:

    currency-formatter supports multiple locales, allowing developers to format currency according to regional standards. This feature is essential for applications targeting international users, ensuring that currency displays are appropriate for different cultures.

  • currency.js:

    currency.js has limited localization support primarily focused on formatting. It does not provide extensive locale management, so developers may need to handle localization separately if required.

  • dinero.js:

    dinero.js supports localization by allowing you to specify currency codes and formats. It is well-suited for applications that need to handle multiple currencies and ensure that values are displayed correctly based on user preferences.

  • numeral:

    numeral provides some localization features, but its primary focus is on number formatting rather than currency-specific localization. Developers may need to implement additional logic for comprehensive localization.

Immutability

  • currency-formatter:

    currency-formatter does not deal with mutability as it is primarily a formatting library. It does not create or manage state, so immutability is not a concern.

  • currency.js:

    currency.js does not enforce immutability, which means that operations can change the state of currency objects. This can lead to potential issues if not managed carefully, especially in complex applications.

  • dinero.js:

    dinero.js emphasizes immutability, meaning that every operation returns a new instance of the money object rather than modifying the original. This feature helps prevent unintended side effects and makes it easier to manage state in applications.

  • numeral:

    numeral does not focus on immutability. It is primarily a formatting library, and while it can handle numbers, it does not enforce immutability in its operations.

Complexity and Learning Curve

  • currency-formatter:

    currency-formatter is straightforward and easy to use, making it suitable for developers who need quick currency formatting without a steep learning curve. Its API is simple and intuitive, allowing for rapid integration.

  • currency.js:

    currency.js has a moderate learning curve due to its focus on arithmetic operations. Developers need to understand how to use its methods effectively to manage currency calculations, but it is still relatively easy to pick up for those familiar with JavaScript.

  • dinero.js:

    dinero.js may have a steeper learning curve due to its comprehensive feature set and focus on immutability. Developers may need to invest time in understanding its API and concepts, but it provides powerful tools for managing money effectively.

  • numeral:

    numeral is easy to learn and use, especially for those familiar with number formatting in JavaScript. Its flexible API allows for quick implementation, making it accessible for developers of all skill levels.

How to Choose: currency-formatter vs currency.js vs dinero.js vs numeral

  • currency-formatter:

    Choose currency-formatter if you need a lightweight solution specifically for formatting currency values with options for different locales and currency symbols. It is ideal for simple applications where you only need to display currency without complex calculations.

  • currency.js:

    Opt for currency.js if you require a library that allows for precise arithmetic operations on currency values. It is particularly useful for applications that need to perform calculations while maintaining accuracy, such as e-commerce platforms or financial applications.

  • dinero.js:

    Select dinero.js if you need a robust library for handling money and currency calculations with a focus on immutability and chainable methods. It is suitable for applications that require complex financial computations and want to avoid issues related to floating-point arithmetic.

  • numeral:

    Use numeral if you need a versatile library for formatting numbers in various ways, including currency. It is great for applications that require extensive number formatting options beyond just currency, such as percentages and custom formats.

README for currency-formatter

Currency Formatter

Build Status

A simple Javascript utility that helps you to display currency properly

STOP! You probably don't need this library

TL;DR: This library was created a long time ago. You should use Internationalization API instead.

Please don't add another dependency which you don't need. All modern browsers (and node.js) have this functionality built-in and do a much better job at formatting currencies. e.g. #57

Example:

new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(100000000)
// => "$100,000,000.00"

new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR' }).format(100000000)
// => "€100,000,000.00"

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'USD' }).format(100000000)
// => "100.000.000,00 $"

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(100000000)
// => "100.000.000,00 €"

new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(100000000)
// => "100 000 000,00 €"

With that being said use this library if you need:

  • Your version of node.js doesn't come with the full-icu. See: #72 and #19214
  • Support old browsers.
  • Consistent formatting across all browsers.
  • You don't like the Intl APIs
  • ???

Install

npm install currency-formatter --save

Basic Usage

By specifying the currency code

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

currencyFormatter.format(1000000, { code: 'USD' });
// => '$1,000,000.00'

currencyFormatter.format(1000000, { code: 'GBP' });
// => '£1,000,000.00'

currencyFormatter.format(1000000, { code: 'EUR' });
// => '1 000 000,00 €'

Or by specifying the locale

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

currencyFormatter.format(1000000, { locale: 'en-US' });
// => '$1,000,000.00'

currencyFormatter.format(1000000, { locale: 'en-GB' });
// => '£1,000,000.00'

currencyFormatter.format(1000000, { locale: 'GB' });
// => '£1,000,000.00'

currencyFormatter.format(1000000, { locale: 'de-DE' });
// => '1.000.000,00 €'

currencyFormatter.format(1000000, { locale: 'nl-NL' });
// => '€1.000.000,00'

You can also get the currency information.

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

currencyFormatter.findCurrency('USD');
// returns:
// {
//   code: 'USD',
//   symbol: '$',
//   thousandsSeparator: ',',
//   decimalSeparator: '.',
//   symbolOnLeft: true,
//   spaceBetweenAmountAndSymbol: false,
//   decimalDigits: 2
// }

Parse the number of a monetary value


currencyFormatter.unformat('$10.5', { code: 'USD' })
// => 10.5

currencyFormatter.unformat('$1,000,000', { code: 'USD' })
// => 1000000

currencyFormatter.unformat('10,5 €', { code: 'EUR' })
// => 10.5

currencyFormatter.unformat('1 000 000,00 €', { code: 'EUR' })
// => 1000000

currencyFormatter.unformat('1.000,99', { locale: 'de-DE' })
// => 1000.99

currencyFormatter.unformat('10\'000 CHF', { code: 'CHF' })
// => 10000

currencyFormatter.unformat('10.00 CHF', { code: 'CHF' })
// => 10

currencyFormatter.unformat('10,00 CHF', { code: 'CHF' })
// => 1000

Advanced Usage

Currency Formatter uses accounting library under the hood, and you can use its options to override the default behavior.

var currencyFormatter = require('currency-formatter');
currencyFormatter.format(1000000, {
  symbol: '@',
  decimal: '*',
  thousand: '^',
  precision: 1,
  format: '%v %s' // %s is the symbol and %v is the value
});

// => '1^000^000*0 @'

// Different formatting for positive and negative values
currencyFormatter.format(-10, {
  format: {
    pos: '%s%v' // %s is the symbol and %v is the value
    neg: '(%s%v)',
    zero: '%s%v'
  }
});

// => ($10)

You could also get a list of all the currencies here using one of the following:

var currencies = require('currency-formatter/currencies');
// OR
var currencyFormatter = require('currency-formatter');
var currencies = currencyFormatter.currencies;

Or the currencies in hashmap shape:

var currencies = require('currency-formatter/currencies.json');
// Result:
// {
//  "USD": {
//    "code": "USD",
//    "symbol": "$",
//    "thousandsSeparator": ",",
//    "decimalSeparator": ".",
//    "symbolOnLeft": true,
//    "spaceBetweenAmountAndSymbol": false,
//    "decimalDigits": 2
//  },
//  ...more currencies
// }

License

MIT