Currency conversion and money formating libraries in Javascript

money.js

These are 2 very useful Javascript libraries for dealing with money currency and formatting written by Joss Crowcroft.

Currency conversion with money.js

money.js is a currency conversion library with no dependencies, in just over 1 KB. You could provide your own currency rates in JSON format or use the data from the Open Source Exchange Rates project which is also created by Joss Crowcroft and looks very promising!

Usage examples:

// From any currency, to any currency:
fx.convert(12.99, {from: "GBP", to: "HKD"});

// Chaining:
fx(1000).from("USD").to("GBP");
fx(1000).to("AED");

// With simple settings and defaults, making this possible:
fx.convert(5318008);
fx(5318008).to("AED");

Money formatting with accounting.js

accounting.js is a small library for number, money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has no dependencies.

I've been using accounting.js for a month -in the administration panel of an e-commerce system- and had zero problems so far.

Usage examples:

// Default usage:
accounting.formatMoney(12345678); // $12,345,678.00

// European formatting (custom symbol and separators), could also use options object as second param:
accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99

// Negative values are formatted nicely, too:
accounting.formatMoney(-500000, "£ ", 0); // £ -500,000

// Simple `format` string allows control of symbol position [%v = value, %s = symbol]:
accounting.formatMoney(5318008, { symbol: "GBP",  format: "%v %s" }); // 5,318,008.00 GBP

// Formatting numbers
accounting.formatNumber(5318008); // 5,318,008
accounting.formatNumber(9876543.21, 3, " "); // 9 876 543.210

// Unformatting numbers / currency strings
accounting.unformat("£ 12,345,678.90 GBP"); // 12345678.9

Comments

Jason  •  March 6, 2013 03:49

accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99 -> this is useless.

Fredy  •  October 4, 2013 12:07

@Jason
I think you do not see the big picture

if you replace the 4999.99 by a variable then this function is very useful

Post your comment

(optional)
(optional, will not be shown)
NOTE: Any links will have rel="nofollow"

back