[Javascript] thousand separator (wrapping up)

Shawn Milo milo at linuxmail.org
Fri Jun 4 15:04:59 CDT 2004


Well, I think I'm done with this function, if for no
other reason than the fact that the OP asked for 
specific functionality, and we've got so far beyond
meeting their requirements that it's not even funny.  :o)

So, if you want to take this and run with it, please feel
free.  You don't even have to keep my initials in it.  I
originally put them in there to separate mine from yours
when I was testing them both, not because of vanity.

It's been a lot of fun, though.  I look forward to 
working with you, and the rest of the group, on the
next challenge.  

Shawn




----- Original Message -----
From: Paul Novitski <paul at novitskisoftware.com>
Date: Fri, 04 Jun 2004 12:21:13 -0700
To: "[JavaScript List]" <javascript at LaTech.edu>
Subject: Re: [Javascript] thousand separator

> Shawn,
> 
> [I fear this thread will become tiresome to others on the list, so if 
> anyone complains perhaps we should move it to the multi-lingual listserve 
> 'multiweb' at googlegroups.com.  I do find the subject quite interesting.]
> 
> Your latest number-formatting script was definitely an improvement.  I have 
> these suggestions:
> _______________________________
> 
> 1) The test for Chinese & Japanese should be /(zh-|ja)/ to cover the codes 
> ja, zh-tw, zh-cn, zh-hk, & zh-sg.  MSDN list of language codes:
> http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/language_codes.asp
> _______________________________
> 
> 2) To make the function more flexible, I'd add an optional argument whereby 
> the caller can specify the language code:
> 
> function addCommas_skm_pdn(someNum, langCode){
> 
> 	if (langCode)
> 	{
> 		var regionTest = langCode
> 	} else {
> 		var regionTest = navigator.systemLanguage + '' + navigator.language
> 	}
> 
> That way, the function can be used to display different nation's currency 
> formats regardless of the local settings of the user's browser.
> _______________________________
> 
> 3) Supporting the Indian pattern (separating the first group of 3 then 2 
> thereafter) is easy if both match & replace RegExps are dynamic during the 
> loop, inserting the proper Group value with each iteration.  Pseudocode:
> 
> - default Group = 3
> - zh/ja Group = 4
> 
> while (match)
> 	- replace one
> 	- if Hindi, Group = 2
> 	- modify match & replace RegExps
> loop
> _______________________________
> 
> 4) The zh/ja group separator, now that I read the fine print, changes to a 
> new character for each group of 10,000; see:
> http://www.xencraft.com/resources/multi-currency.html#ja-count
> 
> This can be handled easily:
> 
> [a]
> // create array of Chinese/Japanese separators
> var aZhJa = new array(U+4E07, U+5104, U+5146, U+4EAC, U+5793, U+79BE, 
> U+7A63, U+6E9D, U+6F97, U+6B63, U+8F09, U+6975);
> 
> var iteration = 0;
> 
> [b]
> while (someNum.match(reMatch))
> {
> 		// change the zh/ja separator each time
> 		if (regionTest.match(/(zh-|ja)/i))
> 		{ strThou = aZhJa[iteration];}
> [b]
> 	someNum = someNum.replace(reReplace, '$1' + strThou + '$2');
> 	iteration++;
> }
> 
> Notes:
> 
> [a] I'm unsure how to represent & manipulate UTF-16 character codes in 
> JavaScript; I'll have to read up on that.
> 
> [b] The ZH/JA separators need to find their way into the match() & 
> replace() regexp's, either by loading them all in from the start or by 
> modifying the regexp's after each replacement.
> _______________________________
> 
> 5) To keep the loop code from getting too gnarly, it might be desireable to 
> separate the formatting into two processes: positioning the separators and 
> determining their value.  For example, one could insert "[#]" each time 
> where # = the iteration number, then replace those afterward with logic 
> like this:
> 
> 	var iteration = 0;
> 
> 	while (someNum.match(reMatch))
> 	{
> 		someNum = someNum.replace(reReplace, '$1\[' + iteration + '\]$2');
> 		iteration++;
> 	}
> 
> before: 1234567890
> 
> after:  1[3]234[2]567[1]890
> or:     1[4]23[3]45[2]67[1]890 (Hindi)
> or:     12[2]3456[1]7890 (Chinese/Japanese)
> 
> then: 	replace each [#] with separator
> 		if zh/ja, different character each time
> 		else single character per language group
> 
> 	// if Zh/Ja, replace each separator with a different character
> 	if (regionTest.match(/zh|ja/i)
> 	{
> 		for (var i=0; i < aZhJa.length; i++)
> 		{
> 			reReplace = "[\" + i + "\]"
> 			reReplace = new RegExp(reReplace)
> 			someNum = someNum.replace(reReplace, aZhJa[i])
> 		}
> 	// everyone else uses the same separator throughout
> 	} else {
> 		someNum = someNum.replace(/\[\d\]/g, sThou)
> 	}
> _______________________________
> 
> Finally, here are some links I've found useful while playing with this:
> 
> Language Codes (MSDN)
> http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/language_codes.asp
> 
> Language Codes:  ISO 639, Microsoft and Macintosh (UNICODE)
> http://www.unicode.org/onlinedat/languages.html
> 
> Country Codes:  ISO 3166, Microsoft, and Macintosh (UNICODE)
> http://www.unicode.org/onlinedat/countries.html
> 
> Currency Internationalization, Multiple Currencies and Foreign Exchange 
> (XenCraft.com)
> http://www.xencraft.com/resources/multi-currency.html
> 
> Detect foreign language support using JavaScript (article)
> http://builder.com.com/5100-6371-5069931.html
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list