[Javascript] thousand separator

Shawn Milo milo at linuxmail.org
Fri Jun 4 11:52:25 CDT 2004


Paul,

   Is this international enough for you?  :o)


Shawn



function addCommas_skm(someNum){

   var strThou = '.';
   var regionTest = navigator.systemLanguage + '' + navigator.language;
   var iGroup = '3';
   var reMatch;
   var reReplace;

   //For China & Japan, separate every 4 digits
   if (regionTest.match(/-(ch|jp)/i)){ iGroup = '4';}

   reMatch = '^\\d\\d{' + iGroup + '}';
   reMatch = new RegExp(reMatch);

   reReplace = '(\\d)(\\d{' + iGroup + '}(\\.|,|$))';
   reReplace = new RegExp(reReplace);

   if (regionTest.match(/en-(au|ca|us)/i)){ strThou = ',';}
   if (regionTest.match(/fr-/i)){ strThou = ' ';}
   if (regionTest.match(/-ch/i)){ strThou = "'";}

   while (someNum.match(reMatch)){
      someNum = someNum.replace(reReplace, '$1' + strThou + '$2');
   }
   return someNum;

}




More information about the Javascript mailing list