简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM