All the other javascript functions I have found do not properly format the "$" into the parenthesis. Here is a formatnumber and a format currency function that will take care of everything for you:
function FormatCurrency(num, decimalNum, bolLeadingZero, bolParens) {
num = FormatNumber(num, decimalNum, bolLeadingZero, bolParens);
if (num.charAt(0) == "(") {
num = num.replace("(", "($");
}
else {
num = "$" + num
}
return num;
}
function FormatNumber(num, decimalNum, bolLeadingZero, bolParens)
/* IN - num: the number to be formatted
decimalNum: the number of decimals after the digit
bolLeadingZero: true / false to use leading zero
bolParens: true / false to use parenthesis for - num
RETVAL - formatted number
*/
{
var tmpNum = num;
// Return the right number of decimal places
tmpNum *= Math.pow(10, decimalNum);
tmpNum = Math.floor(tmpNum);
tmpNum /= Math.pow(10, decimalNum);
var tmpStr = new String(tmpNum);
// See if we need to hack off a leading zero or not
if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
if (num > 0)
tmpStr = tmpStr.substring(1, tmpStr.length);
else
// Take out the minus sign out (start at 2)
tmpStr = "-" + tmpStr.substring(2, tmpStr.length);
// See if we need to put parenthesis around the number
if (bolParens && num < 0)
tmpStr = "(" + tmpStr.substring(1, tmpStr.length) + ")";
return tmpStr;
}
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function StripNumber(number) {
number = number.replace("(", "-");
number = number.replace(")", "");
number = number.replace(",", "");
number = number.replace("$", "");
return number;
}