CurrencyFormatter.js

A super simple currency formatting library

155 currencies, 715 locales & less than 7KB gzipped :)

Works well with unusual formats, including the Indian Rupee

Made with ♡ by

About

CurrencyFormatter.js allows you to format numbers as currencies. It contains 155 currency definitions and 715 locale
definitions out of the box. It handles unusually formatted currencies, such as the INR. This library is now used
by hedge funds, banks and a variety of other organisations across the world for the efficient formatting
of currencies in a wide range of applications.

This library was originally created by the Bx team while developing Bx @ https://usebx.com. Having found no library capable of formatting the more unusual currencies
they decided to create their own and release it under an MIT Licence.
&rarrhk; Give Bx a try if you ever need a tool for invoicing, expenses or project management :)

This library is maintained by OSREC Technologies in London. Bugs and issues
should be reported via github. If you use the library, we appreciate a mention or a link back to https://osrec.co.uk :)

Usage

› Basic Formatting

Formatting a number is pretty straightforward with the OSREC.CurrencyFormatter.format(number, parameters) method. Simply pass in your number along with the required parameters.
The parameters can include the following:

Usually, you will not need to specify all the parameters, and can simply specify the currency (and the locale, if needed). The library is aware of the appropriate format to use for every currency and locale.

› Format all element values as currency

If you need to convert all elements that contain numerical values into well formatted currencies, you can do this easily using
the OSREC.CurrencyFormatter.formatAll(parameters) method. Simply pass in your element selector via the selector parameter, along with the currency
(you can also override a whole bunch of additional parameters - see above for more details).

› Fully bespoke data formatter

The OSREC.CurrencyFormatter.getFormatter(parameters) method returns a bespoke formatting function that can be used to format currencies. This is the most efficient way to format a large number of currencies.

HTML

<input id='frenchEuroInput' value='78234564.5815899' />

JavaScript

// Once generated, the formatter below can used over
// and over again to format any number of currencies
var frenchEuroFormatter = OSREC.CurrencyFormatter.getFormatter
({
// If currency is not supplied, defaults to USD
currency: 'EUR',
// Use to override the currency's default symbol
symbol: '€',
// Use to override the currency's default locale - every locale has
// preconfigured decimal, group and pattern
locale: 'fr',
// Use to override the locale's default decimal character
decimal: ',',
// Use to override the locale's default group (thousand separator) character
group: '.',
// Use to override the locale's default display pattern
// Note comma = group separator, dot = decimal separator, exclamation = symbol
// Follows standard unicode currency pattern
pattern: '#,##0.00 !',
// Return this value if the input value is not a valid number
// Defaults to '0'
valueOnError: '0'
});
var val = document.getElementById('frenchEuroInput').value;
var formattedVal = frenchEuroFormatter(val);
document.getElementById('frenchEuroInput').value = formattedVal;

Result

› Negative and Zero Formats

If you need to specify separate formats for negative numbers, positive numbers and zeros, you may do so by splitting them with a semi-colon like so: positivePattern;negativePattern;zeroPattern.
The example below defines a bespoke pattern for the Swiss Franc (CHF), where the negative numbers are formatted with enclosing brackets and zeros are formatted to 2 decimal places.

› Post Format Function

Sometimes you may wish to post process your formatted numbers. For example, to make negative numbers appear red and positive numbers green. This can be easily done via the postFormatFunction option.
This function will be called, passing in the numerical value, along with the formatted string as arguments. NOTE: the function will NOT be called if the input value is not a valid number.