The library—you can check it out on github—provides two functions,
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);
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);