3

I want to format numbers. I have seen some of the regex expression example to insert comma in number string. All of them check 3 digits in a row and then insert comma in number. But i want something like this:

122 as 122
1234 as 1,234
12345 as 12,345
1723456 as 17,23,456
7123456789.56742 as 7,12,34,56,789.56742

I am very new to regex expression. Please help me how to display the number as the above. I have tried the below method. This always checks for 3 digits and then add comma.

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

But i want comma every 2 digits except for the last 3 digits before the decimals as shown above.

11
  • u want answer in regex or javascript.. or tell what u have tried?? Commented May 30, 2012 at 5:42
  • @Learner : In which ever way it is simple. Commented May 30, 2012 at 5:43
  • @Learner What do you mean "in regex or javascript"? The answer can, and likely will, be both. Commented May 30, 2012 at 5:43
  • @Learner : Yes i know. I mean to say i dont want any complicated solution because it is pretty simple and can be done in few lines though am not able to do it properly Commented May 30, 2012 at 5:48
  • 1
    @user850234 Then show us your solution and we will help you figure out the problem. That will help you to learn the language and we won't have to do YOUR work. Commented May 30, 2012 at 5:54

4 Answers 4

11

The result will depend on your browsers locale. But this might be an acceptable solution:

(7123456789.56742).toLocaleString();

Outputs:

7,123,456,789.56742

Try it and see if it outputs 7,12,34,56,789.567421 in your locale.

Sign up to request clarification or add additional context in comments.

2 Comments

@Jack : Sorry i saw now i have written the wrong comment. Thanks for noticing.
No i am not getting the output 7,12,34,56,789.567421
4

Here's a function to convert a number to a european (1.000,00 - default) or USA (1,000.00) style:

function sep1000(somenum,usa){
  var dec = String(somenum).split(/[.,]/)
     ,sep = usa ? ',' : '.'
     ,decsep = usa ? '.' : ',';
  return dec[0]
         .split('')
         .reverse()
         .reduce(function(prev,now,i){
                   return i%3 === 0 ? prev+sep+now : prev+now;}
                )
         .split('')
         .reverse()
         .join('') +
         (dec[1] ? decsep+dec[1] :'')
  ;
}

Alternative:

function sep1000(somenum,usa){
  var dec = String(somenum).split(/[.,]/)
     ,sep = usa ? ',' : '.'
     ,decsep = usa ? '.' : ',';

  return xsep(dec[0],sep) + (dec[1] ? decsep+dec[1] :'');

  function xsep(num,sep) {
    var n = String(num).split('')
       ,i = -3;
    while (n.length + i > 0) {
        n.splice(i, 0, sep);
        i -= 4;
    }
    return n.join('');
  }
}
//usage for both functions
alert(sep1000(10002343123.034));      //=> 10.002.343.123,034
alert(sep1000(10002343123.034,true)); //=> 10,002,343,123.034

[edit based on comment] If you want to separate by 100, simply change i -= 4; to i -= 3;

function sep100(somenum,usa){
  var dec = String(somenum).split(/[.,]/)
     ,sep = usa ? ',' : '.'
     ,decsep = usa ? '.' : ',';

  return xsep(dec[0],sep) + (dec[1] ? decsep+dec[1] :'');

  function xsep(num,sep) {
    var n = String(num).split('')
       ,i = -3;
    while (n.length + i > 0) {
        n.splice(i, 0, sep);
        i -= 3;  //<== here
    }
    return n.join('');
  }
}

1 Comment

Sorry actually want 10,00,23,43,123.034 and not 10,002,343,123.034 so removed the accepted answer
3

use toLocaleString(); It automatically handles inserting commas and will also handle uk strings the right way

e.g. var num=63613612837131; alert(num.toLocaleString());

Comments

0

Below is the snippet of code, can be done in better way but this works :D

function formatDollar(num) 
{
var p = num.toFixed(2).split(".");
var chars = p[0].split("").reverse();
var sep1000 = false;
var newstr = '';
var count = 0;
var count2=0;
for (x in chars) 
{ 
count++;
if(count%3 == 1 && count != 1 %% !sep1000) 
{ 
newstr = chars[x] + ',' + newstr;
sep1000=true;
} 
else 
{ 
if(!sep1000)
{
newstr = chars[x] + ',' + newstr;
}
else
{
count2++;
if(count%2 == 0 && count != 1) 
{ 
newstr = chars[x] + ',' + newstr;
} 
else 
{ 
 newstr = chars[x] + newstr;
}
}
}
}
return newstr + "." + p[1]; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.