Currency Formatting javascript library available on Github

In addition to all of the small modules we write for client projects, we also write quite a few small javascript libraries. Several times now, we've had a need for good currency formatting in javascript. On the most recent occasion, rather than re-use previously-written code that might or might not be a good fit with the current project, we decided to write a small, flexible, general-purpose, javascript currency formatter--something like PHP's money_format() function (though with far fewer options).

The library—you can check it out on github—provides two functions, currencyFormat() and currencyUnformat().

The currencyFormat() function will correctly preserve decimal places if they're provided, and accepts arguments for the currency symbol (e.g. "$"), and the thousands separator (e.g. ",") that will be used in the output, and accepts an argument that specifies what character to look for as the decimal marker (e.g "."). The currencyUnformat() function simply removes all non-numerical characters except decimal markers from the incoming string. It also accepts a regular expression specifying which characters to remove as an argument.

The functions are added directly to the String prototype, so you can call them directly on any string.

The functions are expected to be used in conjunction with calculation. In the case of currencyUnformat(), the expected use is similar to this:

// Displays "1234567.89" in an alert.
//
// Note that the function output must be cast to float after
// un-formatting and before performing calculations.
var exCurrency = '$ 1,234,567.89 (annually)',
    exFloat = parseFloat(exCurrency.currencyUnformat());
alert(exFloat);

Similarly, the currencyFormat() function is used this way:

// Displays "$ 1,234,567.89" in an alert.
//
// Note that it's necessary to set the number of decimal
// places on the float and convert it to a string before calling
// the currencyFormat() function.
var exFloat = 1234567.890123456789,
    exCurrency = String(exFloat.toFixed(2)).currencyFormat('$ ', ',', '.');
alert(exCurrency);