intl vs currency.js vs currency-formatter vs accounting-js vs numeral
Currency Formatting and Manipulation
intlcurrency.jscurrency-formatteraccounting-jsnumeralSimilar Packages:
Currency Formatting and Manipulation

Currency formatting and manipulation libraries in JavaScript provide tools for handling monetary values, including formatting numbers as currency, parsing currency strings, and performing arithmetic operations while considering currency-specific rules. These libraries help developers create applications that handle financial data accurately and display it in a user-friendly manner. They often include features like localization, support for multiple currencies, and functions for rounding, calculating taxes, and formatting values according to different cultural conventions. This is essential for e-commerce platforms, financial applications, and any software that deals with monetary transactions.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
intl683,2291,696-799 years agoMIT
currency.js579,6523,357-585 years agoMIT
currency-formatter94,403213-154 years agoMIT
accounting-js33,10111746.9 kB118 months agoMIT
numeral09,731-3489 years agoMIT
Feature Comparison: intl vs currency.js vs currency-formatter vs accounting-js vs numeral

Currency Formatting

  • intl:

    The Intl API provides built-in support for currency formatting based on the user's locale. It is highly accurate and leverages the browser's native capabilities to format currency values according to international standards.

  • currency.js:

    currency.js focuses on precise currency representation and formatting. It allows for customizable formatting, including setting decimal places and currency symbols, while ensuring accuracy in calculations and display.

  • currency-formatter:

    currency-formatter specializes in formatting currency values according to locale and currency code, making it easy to display values accurately for different regions. It supports multiple currencies and allows for quick formatting with minimal configuration.

  • accounting-js:

    accounting-js provides simple currency formatting with customizable symbols, decimal places, and thousand separators. It is straightforward and effective for basic currency display needs.

  • numeral:

    numeral supports currency formatting as one of its features. It allows for customizable currency formats, including setting symbols, decimal places, and thousand separators, making it versatile for various formatting needs.

Precision and Accuracy

  • intl:

    The Intl API does not handle calculations; it focuses on accurate formatting of numbers, dates, and currencies based on locale. For precision in financial applications, additional handling is required during calculations.

  • currency.js:

    currency.js is designed for precision in currency calculations, addressing floating-point issues common in JavaScript. It provides accurate arithmetic operations, making it suitable for financial applications where precision is critical.

  • currency-formatter:

    currency-formatter is primarily focused on accurate formatting rather than calculations. It ensures that currency values are displayed correctly according to the specified locale and currency code, but it does not perform arithmetic operations.

  • accounting-js:

    accounting-js handles precision well for formatting and basic arithmetic operations. However, it is not specifically designed for high-precision calculations, so care should be taken with complex financial computations.

  • numeral:

    numeral provides accurate formatting and manipulation of numbers, but it does not guarantee precision in calculations. It is important to manage precision manually, especially when dealing with large numbers or fractional values.

Arithmetic Operations

  • intl:

    The Intl API does not perform arithmetic operations. It is solely for formatting numbers, dates, and currencies according to locale. Arithmetic must be handled using standard JavaScript methods or other libraries.

  • currency.js:

    currency.js excels in arithmetic operations with currency values. It supports addition, subtraction, multiplication, and division while maintaining precision. The library is designed to handle calculations safely, making it ideal for financial applications.

  • currency-formatter:

    currency-formatter does not provide arithmetic operations. It is focused on formatting currency values rather than performing calculations. For arithmetic, you would need to handle it separately.

  • accounting-js:

    accounting-js supports basic arithmetic operations like addition, subtraction, multiplication, and division. It also provides functions for rounding and formatting numbers, making it useful for simple financial calculations.

  • numeral:

    numeral provides basic arithmetic capabilities, but it is not its primary focus. The library allows for manipulation of numbers, including currency, but for complex calculations, additional handling may be required.

Localization Support

  • intl:

    The Intl API is built for localization and provides robust support for formatting numbers, dates, and currencies according to the user's locale. It is highly flexible and accurate, making it ideal for applications that require comprehensive localization features.

  • currency.js:

    currency.js has basic localization capabilities, allowing for customizable currency formatting. However, it does not provide extensive support for multiple locales or automatic localization based on user settings.

  • currency-formatter:

    currency-formatter provides excellent localization support, allowing currency formatting based on locale and currency code. It is designed to handle multiple currencies and regions, making it easy to display values accurately for different audiences.

  • accounting-js:

    accounting-js offers limited localization support, primarily for formatting numbers and currencies. It allows customization of symbols and separators, but it does not provide built-in support for multiple locales.

  • numeral:

    numeral supports localization to some extent, allowing for custom number and currency formats. However, it does not provide automatic localization based on the user's settings, so developers need to implement it manually.

Ease of Use: Code Examples

  • intl:

    Currency Formatting with Intl API`

    // Format currency using the Intl API
    const amount = 1234567.89;
    const formatted = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
    }).format(amount);
    console.log(formatted); // Output: $1,234,567.89
    
  • currency.js:

    Currency Operations with currency.js

    // Import the library
    import Currency from 'currency.js';
    
    // Create a currency instance
    const amount = Currency(1234.56);
    
    // Format the currency
    console.log(amount.format()); // Output: $1,234.56
    
    // Perform arithmetic operations
    const total = amount.add(100);
    console.log(total.format()); // Output: $1,334.56
    
  • currency-formatter:

    Currency Formatting with currency-formatter

    // Import the library
    import { format, parse } from 'currency-formatter';
    
    // Format a number as currency
    const formatted = format(1234567.89, { code: 'USD' });
    console.log(formatted); // Output: $1,234,567.89
    
    // Parse a currency string
    const parsed = parse('$1,234,567.89');
    console.log(parsed); // Output: 1234567.89
    
  • accounting-js:

    Simple Currency Formatting with accounting-js

    // Import the library
    import accounting from 'accounting-js';
    
    // Format a number as currency
    const formattedCurrency = accounting.formatMoney(1234567.89);
    console.log(formattedCurrency); // Output: $1,234,567.89
    
    // Perform basic arithmetic
    const sum = accounting.add(100, 200);
    console.log(sum); // Output: 300
    
  • numeral:

    Number Formatting with numeral

    // Import the library
    import numeral from 'numeral';
    
    // Format a number as currency
    const formattedCurrency = numeral(1234567.89).format('$0,0.00');
    console.log(formattedCurrency); // Output: $1,234,567.89
    
    // Perform basic arithmetic
    const sum = numeral(100).add(200);
    console.log(sum.value()); // Output: 300
    
How to Choose: intl vs currency.js vs currency-formatter vs accounting-js vs numeral
  • intl:

    Use the built-in Intl API if you want a native solution for internationalization, including currency formatting. It is part of the JavaScript standard and provides robust support for formatting numbers, dates, and currencies according to the user's locale, making it a good choice for applications that prioritize performance and reduce external dependencies.

  • currency.js:

    Opt for currency.js if you need a library that provides precise currency calculations, including addition, subtraction, multiplication, and division, while avoiding floating-point errors. It is suitable for applications that require accurate financial calculations and offers a clear API for working with monetary values.

  • currency-formatter:

    Select currency-formatter if you want a simple and efficient library for formatting currency values with support for multiple currencies and localization. It is easy to use and integrates well into projects that need quick currency formatting without extensive features.

  • accounting-js:

    Choose accounting-js if you need a lightweight library focused on simple currency formatting, number formatting, and basic arithmetic operations. It is ideal for projects that require minimal dependencies and straightforward functionality.

  • numeral:

    Choose numeral if you need a versatile library for formatting and manipulating numbers, including currency formatting. It offers a wide range of formatting options and is suitable for applications that require more than just currency handling, such as custom number formats and data visualization.

README for intl

Intl.js Build Status

In December 2012, ECMA International published the first edition of Standard ECMA-402, better known as the ECMAScript Internationalization API. This specification provides the framework to bring long overdue localization methods to ECMAScript implementations.

All modern browsers, except safari, have implemented this API. Intl.js fills the void of availability for this API. It will provide the framework as described by the specification, so that developers can take advantage of the native API in environments that support it, or Intl.js for legacy or unsupported environments.

Getting started

Intl.js and FT Polyfill Service

Intl.js polyfill was recently added to the Polyfill service, which is developed and maintained by a community of contributors led by a team at the Financial Times. It is available through cdn.polyfill.io domain, which routes traffic through Fastly, which makes it available with global high availability and superb performance no matter where your users are.

To use the Intl polyfill through the Polyfill service just add one script tag in your page before you load or parse your own JavaScript:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>

When specifying the features to use through the polyfill service, you have to specify what locale, or locales to load along with the Intl polyfill for the page to function, in the example above we are specifying Intl.~locale.en, which means only en, but you could do something like this:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.fr,Intl.~locale.pt"></script>

note: the example above will load the polyfill with two locale data set, fr and pt.

This is by far the best option to use the Intl polyfill since it will only load the polyfill code and the corresponding locale data when it is really needed (e.g.: safari will get the code and patch the runtime while chrome will get an empty script tag).

Intl.js and Node

For Node.js applications, you can install intl using NPM:

npm install intl

Node.js 0.12 has the Intl APIs built-in, but only includes the English locale data by default. If your app needs to support more locales than English, you'll need to get Node to load the extra locale data, or use intl npm package to patch the runtime with the Intl polyfill. Node.js versions prior to 0.12 and ≥v3.1 don't provide the Intl APIs, so they require that the runtime is polyfilled.

The following code snippet uses the intl polyfill and intl-locales-supported npm packages which will help you polyfill the Node.js runtime when the Intl APIs are missing, or if the built-in Intl is missing locale data that's needed for your app:

var areIntlLocalesSupported = require('intl-locales-supported');

var localesMyAppSupports = [
    /* list locales here */
];

if (global.Intl) {
    // Determine if the built-in `Intl` has the locale data we need.
    if (!areIntlLocalesSupported(localesMyAppSupports)) {
        // `Intl` exists, but it doesn't have the data we need, so load the
        // polyfill and patch the constructors we need with the polyfill's.
        var IntlPolyfill    = require('intl');
        Intl.NumberFormat   = IntlPolyfill.NumberFormat;
        Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
    }
} else {
    // No `Intl`, so use and load the polyfill.
    global.Intl = require('intl');
}

Intl.js and Browserify/webpack

If you build your application using browserify or webpack, you will install intl npm package as a dependency of your application. Ideally, you will avoid loading this library if the browser supports the built-in Intl. An example of conditional usage using webpack might look like this:

function runMyApp() {
    var nf = new Intl.NumberFormat(undefined, {style:'currency', currency:'GBP'});
    document.getElementById('price').textContent = nf.format(100);
}
if (!global.Intl) {
    require.ensure([
        'intl',
        'intl/locale-data/jsonp/en.js'
    ], function (require) {
        require('intl');
        require('intl/locale-data/jsonp/en.js');
        runMyApp()
    });
} else {
    runMyApp()
}

note: a similar approach can be implemented with browserify, althought it does not support require.ensure.

note: the locale data is required for the polyfill to function when using it in a browser environment, in the example above, the english (en) locale is being required along with the polyfill itself.

Intl.js and Bower

Intl.js is also available as a Bower component for the front-end:

bower install intl

Then include the polyfill in your pages as described below:

<script src="path/to/intl/Intl.js"></script>
<script src="path/to/intl/locale-data/jsonp/en.js"></script>

note: use the locale for the current user, instead of hard-coding to en.

Status

Current progress is as follows:

Implemented

  • All internal methods except for some that are implementation dependent
  • Checking structural validity of language tags
  • Canonicalizing the case and order of language subtags
  • Intl.NumberFormat
    • The Intl.NumberFormat constructor (11.1)
    • Properties of the Intl.NumberFormat Constructor (11.2)
    • Properties of the Intl.NumberFormat Prototype Object (11.3)
    • Properties of Intl.NumberFormat Instances(11.4)
  • Intl.DateTimeFormat
    • The Intl.DateTimeFormat constructor (12.1)
    • Properties of the Intl.DateTimeFormat Constructor (12.2)
    • Properties of the Intl.DateTimeFormat Prototype Object (12.3)
    • Properties of Intl.DateTimeFormat Instances(12.4)
  • Locale Sensitive Functions of the ECMAScript Language Specification
    • Properties of the Number Prototype Object (13.2)
    • Properties of the Date prototype object (13.3)

Not Implemented

  • BestFitSupportedLocales internal function
  • Implementation-dependent numbering system mappings
  • Calendars other than Gregorian
  • Support for time zones
  • Collator objects (Intl.Collator) (see below)
  • Properties of the String prototype object

A few of the implemented functions may currently be non-conforming and/or incomplete.
Most of those functions have comments marked as 'TODO' in the source code.

The test suite is run with Intl.Collator tests removed, and the Collator constructor removed from most other tests in the suite. Also some parts of tests that cannot be passed by a JavaScript implementation have been disabled, as well as a small part of the same tests that fail due to this bug in v8.

What about Intl.Collator?

Providing an Intl.Collator implementation is no longer a goal of this project. There are several reasons, including:

  • The CLDR convertor does not automatically convert collation data to JSON
  • The Unicode Collation Algorithm is more complicated that originally anticipated, and would increase the code size of Intl.js too much.
  • The Default Unicode Collation Element Table is huge, even after compression, and converting to a native JavaScript object would probably make it slightly larger. Server-side JavaScript environments will (hopefully) soon support Intl.Collator, and we can't really expect client environments to download this data.

Compatibility

Intl.js is designed to be compatible with ECMAScript 3.1 environments in order to follow the specification as closely as possible. However, some consideration is given to legacy (ES3) environments, and the goal of this project is to at least provide a working, albeit non-compliant implementation where ES5 methods are unavailable.

A subset of the tests in the test suite are run in IE 8. Tests that are not passable are skipped, but these tests are mostly about ensuring built-in function behavior.

Locale Data

Intl.js uses the Unicode CLDR locale data, as recommended by the specification. The main Intl.js file contains no locale data itself. In browser environments, the data should be provided, passed into a JavaScript object using the Intl.__addLocaleData() method. In Node.js, or when using require('intl'), the data is automatically added to the runtime and does not need to be provided.

Contents of the locale-data directory are a modified form of the Unicode CLDR data found at http://www.unicode.org/cldr/.

## RegExp cache / restore Intl.js attempts to cache and restore static RegExp properties before executing any regular expressions in order to comply with ECMA-402. This process is imperfect, and some situations are not supported. This behavior is not strictly necessary, and is only required if the app depends on RegExp static properties not changing (which is highly unlikely). To disable this functionality, invoke Intl.__disableRegExpRestore().

Contribute

See the CONTRIBUTING file for info.

License

Copyright (c) 2013 Andy Earnshaw

This software is licensed under the MIT license. See the LICENSE.txt file accompanying this software for terms of use.