1

I wanted 1000 to look like 10 000. There are tons of examples to make a separator but they all show you how to start using comma or some StringLocal. How do I use space instead? Which locale should I use?

I have already explained how mine question is different. I have asked it just because that solution does not suit me. I am not happy with commas. It is rediculous to hear that crucial difference = the question is duplicate.

Little Alien
  • 1
  • 8
  • 22
  • If you need this done, it is better to do it manually. I'm going to link to another question with some very good answers and vote to close this as a duplicate. Just replace the comma with a space. – Jeremy J Starcher Jun 03 '16 at 18:20
  • Possible duplicate of [How to print a number with commas as thousands separators in JavaScript](http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Jeremy J Starcher Jun 03 '16 at 18:21
  • I do not see the reason not to let people to know the best solution. The fact that we can build on another solution does not mean that the question is the same. – Little Alien Jun 03 '16 at 18:53
  • 4
    Much of the same code that can be used to format the numbers with commas can be used to format the number with spaces. I verified the usefulness of the answers in that question before I posted it. The very first answer in the linked question will do what you want.. `return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");` Just replace the comma with a space. – Jeremy J Starcher Jun 03 '16 at 19:01
  • Yes, it works!! https://jsfiddle.net/mjvpwes2/ – Little Alien Jun 03 '16 at 19:17

1 Answers1

1

Here is my solution:

var number = 15000; //Put your number
var numstring = number.toString();

if(numstring.length > 3){
    var thpos = -3;
    var strgnum = numstring.slice(0, numstring.length+thpos);
    var strgspace = (" " + numstring.slice(thpos));
    numstring = strgnum + strgspace;
}

console.log(numstring);
cpinamtz
  • 773
  • 8
  • 13