You can use the following method to format your numbers or currency values. It
takes four arguments: the number to format, the number of decimal places, a
Boolean to indicate whether zeros should be appended, and a Boolean to indicate
whether commas should be inserted to separate thousands.



function FormatNumber(num, decimalPlaces, appendZeros, insertCommas) {
var powerOfTen = Math.pow(10, decimalPlaces);
var num = Math.round(num * powerOfTen) / powerOfTen;
if (!appendZeros && !insertCommas)
{
return num;
}
else
{
var strNum = num.toString();
var posDecimal = strNum.indexOf(".");
if (appendZeros)
{
var zeroToAppendCnt = 0;
if (posDecimal < 0)
{
strNum += ".";
zeroToAppendCnt = decimalPlaces;
}
else
{
zeroToAppendCnt = decimalPlaces - (strNum.length - posDecimal - 1);
}
for (var i = 0; i < zeroToAppendCnt; i++)
{
strNum += "0";
}
}
if (insertCommas && (Math.abs(num) >= 1000))
{
var i = strNum.indexOf(".");
if (i < 0)
{
i = strNum.length;
}
i -= 3;
while (i >= 1)
{
strNum = strNum.substring(0, i) + ',' + strNum.substring(i, strNum.length);
i -= 3;
}
}
return strNum;
}
}