javascript – format number with grouping digit (regexp)

"222222222222222223222".replace(/(\d)(?=(\d{3})+$)/g, '$1.');

Number.prototype.format = function () {

if ((1.1).toLocaleString().indexOf(".") >= 0) {
return this.toString().split( /(?=(?:\d{3})+(?:\.|$))/g ).join( "," );
}
else {
return this.toString().split( /(?=(?:\d{3})+(?:,|$))/g ).join( "." );
}
};
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to javascript – format number with grouping digit (regexp)

  1. rg443 says:

    update all numbers with digit signs

    function dosomething(s){return(s.replace(/(\d)(?=(\d{3})+$)/g, '$1.'))}
    document.body.innerHTML=document.body.innerHTML.replace(/(\d{4,})/gim,dosomething)
    
  2. rg443 says:
    function readable(a) {
      var b = Math.floor(Math.log(a) / Math.log(1E3)), a = (a / Math.pow(1E3, Math.floor(b))).toFixed(1) + " kMGTP".substr(b, 1);
      return(6 > a.length ? Array(7 - a.length).join(" ") : "") + a + "\t"
    }
    document.body.innerHTML = document.body.innerHTML.replace(/(\d{4,})/gim, readable);
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s