Skip to content

Instantly share code, notes, and snippets.

@williammalo
Forked from 140bytes/LICENSE.txt
Last active October 3, 2015 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williammalo/2363615 to your computer and use it in GitHub Desktop.
Save williammalo/2363615 to your computer and use it in GitHub Desktop.
number splitter

Splits numbers with commas to make them more readable. Thanks to @maettig for being awesome

function(n){
return //return
(""+n) //the number converted to a sting
.replace(/\B(?=(...)+$)/g,",") //with a comma inserted every 3 characters
}
//simple version:
function(n){return(""+n).replace(/\B(?=(...)+$)/g,",")}
//Does not fuck decimals up version:
function f(a,b,c){return b||(c?a+',':a+'').replace(/(\.\d*)|\d(?=(\d{3})+\b)/g,f)}
{
"name": "numSplit",
"description": "Splits numbers with commas to make them more readable.",
"keywords": [
"number",
"comma",
"math",
"format"
]
}
<script>
numSplit = function(n){return(""+n).replace(/\B(?=(...)+$)/g,",")}
document.write(numSplit(3255325235523632))​​​​​
</script>
@atk
Copy link

atk commented Apr 12, 2012

You can floor and string-convert a number easier than with the split stuff; use y=''+~~n, the replace will work with "$1,$2", too, it's not necessary to combine those strings.

@maettig
Copy link

maettig commented Apr 12, 2012

70 69 bytes. I love a good regular expression. I tried @atk's suggestion but it wont work for numbers that exceed the 32 bit integer limit.

function(a){return(''+a).split('.')[0].replace(/\B(?=(...)+$)/g,',')}

@williammalo
Copy link
Author

@maettig
O_O I forgot that "g" exists... what a fail!
Meh, who cares about flooring!?:

function(n){return(""+n).replace(/.(?=(...)+$)/g,"$&,")}

(56 bytes)

@atk
Copy link

atk commented Apr 12, 2012

Now that's a nice one :)

@maettig
Copy link

maettig commented Apr 12, 2012

55 bytes. But this works only if you know your input is an integer.

function(a){return(''+a).replace(/\B(?=(...)+$)/g,',')}

I would love to spend the remaining bytes to make this work with every input including strings like "1234 people are spending 1234567.89 USD each 1.2345 days". The following version (58 bytes) almost works except for numbers with fractions. It outputs "1.2,345". I'm not sure how to fix this.

function(a){return(''+a).replace(/\B(?=(\d{3})+\b)/g,',')}

@maettig
Copy link

maettig commented Apr 12, 2012

It's hard to explain but it works. Edit: Not enough test cases. "12345" becomes "1234,5" because rule number 3 matches first.

  1. Find a dot followed by at least four digits (\.\d{3}\B). Insert a comma after the third digit. Note: \B is a "non-boundary" where a word-character (\w) and another word-character met.
  2. Or find a digit that is followed by groups of 3 digits (\d(\d{3})+\b).
  3. Continue from either 1. or 2. and insert a comma every three digits. Because of the leading \B this only happens if a digit was matched before.
function(a){return(''+a).replace(/(\.|\B)\d{3}\B|\d(?=(\d{3})+\b)/g,'$&,')}

@williammalo
Copy link
Author

@maettig
Thats pretty cool :D
But imo it's kinda pointless.
Feel free to fork tho.

@maettig
Copy link

maettig commented Apr 12, 2012

Making numSplit(1234.5678) work is pointless?

@williammalo
Copy link
Author

@maettig
Well, it doesn't really make it work, it adds commas to decimal places... I don't think thats how english works. Then again, I might be wrong about that.

@atk
Copy link

atk commented Apr 12, 2012

try numSplit(125252.2421415) in Firefox => "1252,52.242,141,5"

Correct would be 125,252.2421415

@maettig
Copy link

maettig commented Apr 12, 2012

You are right. I missed that in my tests. What about this?

function(a){return(''+a).replace(/\.\d*|\B(?=(\d{3})+\b)/g,function(a){return a||','})}

Merging the two functions is possible but makes the code very ugly. Edit: OK, here it is. This is smaller and more reliable because it does not insert a comma in "U100".

function f(a,b,c){return b?b:c?a+',':(''+a).replace(/(\.\d*)|\d(?=(\d{3})+\b)/g,f)}

@williammalo
Copy link
Author

@maettig
Sweet!
1 byte smaller:

function f(a,b,c){return b||(c?a+',':a+'').replace(/(\.\d*)|\d(?=(\d{3})+\b)/g,f)}

@maettig
Copy link

maettig commented Apr 12, 2012

Yep, that's cool. This will start recursive calls of replace but it will stop immediately because a contains single digits only. When you do a benchmark you will see it's slower but this can be ignored because of the fast regular expression engines nowadays.

@atk
Copy link

atk commented Apr 13, 2012

I like it!

@tsaniel
Copy link

tsaniel commented Jan 20, 2013

What about saving 2 bytes with split?

function(n){return''+(''+n).split(/\B(?=(?:...)+$)/)}

@tsaniel
Copy link

tsaniel commented Jan 22, 2013

123456789..toLocaleString() seems doing the trick.

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