[Javascript] thousand separator
Shawn Milo
milo at linuxmail.org
Thu Jun 3 15:09:14 CDT 2004
Paul,
That was some fancy work. I tried to mess with it, but it's hard
for me to work on other people's code. But, you motivated me to
take a good look at my function, and I was able to both fix the bug
which allowed commas after the decimal point, and simplify it quite a bit.
Granted, this still isn't handling European delimiters, but I'll take
a crack at that next. I just wanted to get this out there before I went
home today.
function addCommas_skm(someNum){
while (someNum.match(/^\d\d{3}/)){
someNum = someNum.replace(/(\d)(\d{3}(\.|,|$))/, '$1,$2');
}
return someNum;
}
Shawn
----- Original Message -----
From: Paul Novitski <paul at novitskisoftware.com>
Date: Thu, 03 Jun 2004 12:01:21 -0700
To: "[JavaScript List]" <javascript at LaTech.edu>
Subject: Re: [Javascript] thousand separator
>
> >----- Original Message -----
> >From: "andy susanto" <andy78 at centrin.net.id>
> > > is there any script that can formating number with thousand separator ?,
>
> At 05:06 AM 6/3/2004, Shawn Milo wrote:
> >while (someNum.match(/^(.*\d)(\d{3}(\.|,|$).*$)/)){
> > someNum = someNum.replace(/^(.*\d)(\d{3}(\.|,|$).*$)/, '$1,$2');
> >}
>
>
> Shawn,
>
> Your script had two problems I could see: it inserted thousand separators
> into digits following the decimal point, and the thousands- and
> decimal-separators were hard-coded.
>
> Below is a version I cooked up that takes fixes the bug and allows for the
> programmer to pass thousands- & decimal-separators as optional arguments
> which are incorporated into the RegExp.
>
> My logic contains one kludgey aspect you can probably help me clean up. To
> make my RegExp work, I'm adding a temporary decimal delimiter to the
> integer portion of my number:
> var sInt = aParts[0] + sDec
>
> I'm therefore searching for:
> (\d)(\d{3}(\,|\.))
> (one digit) followed by (three digits) followed by (either thou or dec)
>
> This is because I couldn't make it work by testing for the thousands
> separator OR the end of string as you've done. I've failed to get either
> of these patterns to work:
> (\d)(\d{3}(\,|$))
> (\d)(\d{3}(\,|$).*$)
>
> What am I missing there?
>
> Paul
> _____________________________________
>
> <script type="text/javascript">
>
> function addCommas(argNum, argThouSeparator, argDecimalPoint)
> {
> // default separator values (should resolve to local standard)
> var sThou = (argThouSeparator) ? argThouSeparator : ","
> var sDec = (argDecimalPoint) ? argDecimalPoint : "."
>
> // split the number into integer & fraction
> var aParts = argNum.split(sDec)
>
> // isolate the integer & add enforced decimal point
> var sInt = aParts[0] + sDec
>
> // tests for four consecutive digits followed by a thousands- or
> decimal-separator
> var rTest = new RegExp("(\\d)(\\d{3}(\\" + sThou + "|\\" + sDec +
> "))")
>
> while (sInt.match(rTest))
> {
> // insert thousands-separator before the three digits
> sInt = sInt.replace(rTest, "$1" + sThou + "$2")
> }
>
> // plug the modified integer back in, removing the temporary
> decimal point
> aParts[0] = sInt.replace(sDec, "")
>
> // debug display
> document.write ("<tr>" +
> "<td>" + rTest.source + "</td>" +
> "<td>" + argNum + "</td>" +
> "<td>" + aParts.join(sDec) + "</td>" +
> "</tr>")
>
> // return the modified integer + decimal portion(s)
> return aParts.join(sDec)
> }
>
> // test it
> document.write ("<table border=1>")
>
> document.write ("<tr>" +
> "<th>RegExp</th>" +
> "<th>BEFORE</td>" +
> "<th>AFTER</th>" +
> "</tr>")
>
> addCommas("1")
> addCommas("12")
> addCommas("123")
> addCommas("1234")
> addCommas("1.1")
> addCommas("12.12")
> addCommas("123.123")
> addCommas("1234.1234")
> addCommas("123456.")
> addCommas("1234567890")
> addCommas("0.1234567890")
> addCommas("1234567890.1234567890")
>
> // this final example passes the European standard separators
> addCommas("1234567890,1234567890", ".", ",")
>
> document.write ("</table>")
>
> </script>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
More information about the Javascript
mailing list