[Javascript] thousand separator
Paul Novitski
paul at novitskisoftware.com
Thu Jun 3 14:01:21 CDT 2004
>----- 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>
More information about the Javascript
mailing list