Skip to content

Instantly share code, notes, and snippets.

@jdsharp
Created March 3, 2011 04:17
Show Gist options
  • Save jdsharp/852326 to your computer and use it in GitHub Desktop.
Save jdsharp/852326 to your computer and use it in GitHub Desktop.
Quick and dirty code to insert commas after every 3 digits (also handles decimals correctly)
// via @DmitryBaranovsk
function dirtyCommas(num) {
return String(num).replace(/^\d+(?=\.|$)/, function (int) { return int.replace(/(?=(?:\d{3})+$)(?!^)/g, ","); });
}
@paches86
Copy link

I've been trying several versions of this and it works great with numbers that are not thousand rounded.
for instance, 1234 goes 1,234.00.
But when I have 4000, I get a 4,0.00, so it basically removes 3 zeros off the thousands portion.
Does anyone have a clue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment