I picked up the following script from an old post on this newsgroup. It
works fine for numbers to two decimal places, but I have a need to format
numbers to three decimal spaces from this: 1234.394 to this: 1,234.394 (no
rounding). I don't quite follow the reg exp in the variable fmt or what the
$1 and $2 do in the myNum replace method. Any enlightenment/help would be
appreciated.
Thanks, John
//FORMATS NUMBERS WITH COMMAS.
function formatNum(num){
var fmt=/(-?\d+)(\d{3})/;
var myNum=String(num);
while(fmt.test(myNum))
{
myNum = myNum.replace(fmt, "$1,$2");
}
return myNum;
}
Also I need to limit the user to no more than 3 decimal places as they type
or onchange. Opinions on the best way to accomplish this. IE only ok, but
would like it to work for Mozilla/NS 7 too.