On 10/10/2005, at 19:04, Alejandro Cid wrote:

Tios quiero formatear un numero que saco de un XML, ya lo he convertido a
number, lo que quiero es poner el puntito de miles tal que asi
de "123456" a "123.456", se que es una chorrada, pero a ver que opinais...

Hace poco usé para un proyectillo un prototype de Sephiroth que funciona
    bastante bien. Es un poco farragoso, pero algo es algo :)

    Hope it helps.


    a!e


Number.prototype.number_format = function(decimals, ts, ds)
{
   if(isNaN(this)) return undefined;
   if(decimals < 0) return undefined;
   if(decimals == undefined) decimals = 0;
   if(ts == undefined) ts = ',';
   if(ds == undefined) ds = '.';
   //   ------------------
   //   thousand separator
   //   ------------------
   var returned = this.toString().split('.');
   var str_begin = returned[0];
   var str_after = returned[1];
   var temp_str = new String();
   var i = 0;
   //   ------------------
   //   thousand separator
   //   ------------------
   while(i<str_begin.length)
   {
      temp_str += i%3==0 && i!=0 ?
      ts + substring(str_begin,str_begin.length-i,1) :
      substring(str_begin,str_begin.length-i,1);
      i++;
   }
   //   ----------------------
   //   decimals
   //   if decimals==0 return
   //   ----------------------
   if(decimals>0)
   {
      str_after = substring(str_after,1,decimals);
      if(str_after.length<decimals)
      {
         while(str_after.length<decimals)
         {
            str_after += '0';
         }
      }
   }
   //   ----------------------
   //   join the two strings
   //   ----------------------
   i = temp_str.length;
   returned = new String();
   while(i>0)
   {
      returned += substring(temp_str,i,1);
      i--
   }
   if(decimals>0)
   {
      return (returned + ds + str_after);
   }
   return returned;
}----------------------------------
Lista ASNativos:asnativos@5dms.com
http://www.5dms.com/listas
----------------------------------

Reply via email to