[Javascript] thousand separator

Shawn Milo milo at linuxmail.org
Thu Jun 3 07:06:35 CDT 2004


Okay, I've jury-rigged a solution.  
It ain't pretty, but it works.

I've just used a simplified regex
and a 'while' loop.

Suggestions welcome.

Shawn

      <script type="text/javascript">

         function addCommas(someNum){

            while (someNum.match(/^(.*\d)(\d{3}(\.|,|$).*$)/)){
               someNum = someNum.replace(/^(.*\d)(\d{3}(\.|,|$).*$)/, '$1,$2');
            }
            return someNum;

         }

         alert(addCommas('6541684.23'));
         alert(addCommas('1000.1'));
         alert(addCommas('34562346346'));
         alert(addCommas('3456345634563456'));
         alert(addCommas('763773763567.342'));
         alert(addCommas('123456.234'));

      </script>






----- Original Message -----
From: "andy susanto" <andy78 at centrin.net.id>
Date: Thu, 3 Jun 2004 07:36:03 +0700
To: <javascript at LaTech.edu>
Subject: [Javascript] thousand separator 

> hai,
> 
> is there any script that can formating number with thousand separator ?, 
> 
> if not how to create that script ?
> 
> TIA,
> 
> andy
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list