Little Alien
Little Alien

Reputation: 1

Formatting integers with space as thousands separator

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.

Upvotes: 1

Views: 4167

Answers (1)

cpinamtz
cpinamtz

Reputation: 853

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);

Upvotes: 1

Related Questions