Javascript Number formatted()

Description

Javascript Number formatted()

Number.prototype.formatted = function(){
 if (!this) return 0;
 
 var sign  = this < 0 ? '-' : '',
  num  = Math.floor(Math.abs(this)*100+0.50000000001),
  cents = (num % 100).toString().substring(0, 2);
  
 num = Math.floor(num/100).toString();
 
 for (var i = 0, l = Math.floor((num.length-1)/3), p = 3; i < l; i++, p += 4){
  num = num.substring(0, num.length - p) + ' ' + num.substring(num.length - p);
 }
 
 return sign + num + '.' + (cents < 10 ? '0' + cents : cents);
};



PreviousNext

Related