Format Numbers in javascript in currency format

While working with numbers in currency format, it is quite confusing to format a number to a desired value.

However, we have a javascript method 'toFixed', that lets us easily format numbers for a specific number of trailing decimals.

The toFixed() method rounds the number to a specified number of decimal places.



Here, the parameter "numlength" is required. This function formats the given number for "numlength" number of trailing decimals. The number is rounded up, and "0"s are used after the decimal point if needed to create the desired decimal length.


var currency=1732.9326;
alert(currency.toFixed(3)); //alerts 1732.933 (round up)
alert(currency.toFixed(2)); //alerts 1732.93
alert(currency.toFixed(7)); //alerts 1732.9326000 (padding)