Currency Formatter
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)
new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR' }).format(100000000)
new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'USD' }).format(100000000)
new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(100000000)
new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(100000000)
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' });
currencyFormatter.format(1000000, { code: 'GBP' });
currencyFormatter.format(1000000, { code: 'EUR' });
Or by specifying the locale
var currencyFormatter = require('currency-formatter');
currencyFormatter.format(1000000, { locale: 'en-US' });
currencyFormatter.format(1000000, { locale: 'en-GB' });
currencyFormatter.format(1000000, { locale: 'GB' });
currencyFormatter.format(1000000, { locale: 'de-DE' });
currencyFormatter.format(1000000, { locale: 'nl-NL' });
You can also get the currency information.
var currencyFormatter = require('currency-formatter');
currencyFormatter.findCurrency('USD');
Parse the number of a monetary value
currencyFormatter.unformat('$10.5', { code: 'USD' })
currencyFormatter.unformat('$1,000,000', { code: 'USD' })
currencyFormatter.unformat('10,5 €', { code: 'EUR' })
currencyFormatter.unformat('1 000 000,00 €', { code: 'EUR' })
currencyFormatter.unformat('1.000,99', { locale: 'de-DE' })
currencyFormatter.unformat('10\'000 CHF', { code: 'CHF' })
currencyFormatter.unformat('10.00 CHF', { code: 'CHF' })
currencyFormatter.unformat('10,00 CHF', { code: 'CHF' })
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'
});
currencyFormatter.format(-10, {
format: {
pos: '%s%v'
neg: '(%s%v)',
zero: '%s%v'
}
});
You could also get a list of all the currencies here using one of the following:
var currencies = require('currency-formatter/currencies');
var currencyFormatter = require('currency-formatter');
var currencies = currencyFormatter.currencies;
Or the currencies in hashmap shape:
var currencies = require('currency-formatter/currencies.json');
License
MIT