Re: Formatting Numbers using JavaScript

by "Cyanide _7" <leo7278(at)hotmail.com>

 Date:  Sun, 26 Mar 2000 03:30:55 CST
 To:  hwg-languages(at)hwg.org
  todo: View Thread, Original
sorry, made a little error in the second for loop argument:

function format(num){
  if(isNaN(num))
    num = 0;
  var dol = Math.floor(num).toString();
  var cents = Math.floor((num*100+.5)%100).toString();
  for (var i = 0; i < Math.floor((dol.length-(i+1))/3); i++)
    dol = dol.slice(0,-(4*i+3))+','+dol.slice(-(4*i+3));
  while (cents.length<2)
    cents += "0";
  return "$"+dol+"."+cents;
}

the previous script was gonna leave a few commas out as the number gets 
bigger, sorry. - Cyanide_7
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA