Here’s a little javascript to format numbers like the PHP function “number_format” :
function number_format (number, decimals, dec_point, thousands_sep){
if (isNaN(number) || number=='') number = 0;
var decimals = decimals != undefined ? decimals : 2,
dec_point = dec_point != undefined ? dec_point : '.',
thousands_sep = thousands_sep != undefined ? thousands_sep : ' ',
negative = number<0;
if (negative) number *= -1;
var left = parseInt(number, 10),
right = Math.round(parseFloat(number.toString().replace(/^d+./, '0.')) * Math.pow(10, decimals));
left = left.toString().split('').reverse().join('')
.match(/d{3}|d{1,2}/g)
.join(thousands_sep)
.split('').reverse().join('');
right = (right / Math.pow(10,decimals)).toString().replace(/^d+./, '').toString();
if (right.length < decimals) for (var iRight=right.length; iRight < decimals; iRight++) right += '0';
return (negative?'-':'')+left+dec_point+right;
}
UPDATE : There was a bug in the code. The zeros to the left in the decimals (if there was any) were erased.
UPDATE 2 : There was a second bug, when the number was under 0. Fixed now