Skip to content

Instantly share code, notes, and snippets.

@pastelInc
Created July 12, 2021 03:35
Show Gist options
  • Select an option

  • Save pastelInc/13faae6dd53117d0cbf14ed65f7e8045 to your computer and use it in GitHub Desktop.

Select an option

Save pastelInc/13faae6dd53117d0cbf14ed65f7e8045 to your computer and use it in GitHub Desktop.
String.prototype.number_format = function(d) {
const absValue = Math.abs(d)
let c = isNaN(absValue) ? 2 : d;
let n = this;
let s = n < 0 ? "-" : "";
const i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + ',' : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + ',') + (c ? '.' + Math.abs(n - i).toFixed(c).slice(2) : "");
}
console.log(
'2234324.99'.number_format()
);
//2,234,324.99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment