Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Last active December 11, 2015 06:59
Show Gist options
  • Save atk/4563237 to your computer and use it in GitHub Desktop.
Save atk/4563237 to your computer and use it in GitHub Desktop.
Decimal Points (for integer numbers)

Add decimal markers to any integer.

function(
a, // positive integer number
b, // counter
c // result (array)
){
for(
a += c = [], // coerce a to string, intialize result array
b = a.length; // fill counter with length
(b -= 3) > -3; // decrease counter by 3 and check if > -3
c.unshift(a.substr(b < 0 ? 0 : b, b < 0 ? -b : 3)) // fetch the next 3 (or less) numbers
);
return '' + c // string coercion will automatically add commas (as c.join(',') would)
}
function(a,b,c){for(a+=c=[],b=a.length;(b-=3)>-3;c.unshift(a.substr(b<0?0:b,b<0?-b:3)));return''+c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "theNameOfYourLibWhichMustBeAValidCamelCasedJavaScriptIdentifier",
"description": "This should be a short description of your entry.",
"keywords": [
"five",
"descriptive",
"keywords",
"or",
"fewer"
]
}
<!DOCTYPE html>
<title>Decimal markers</title>
<div>Expected value: <b>12,234,567,890</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
document.getElementById( "ret" ).innerHTML = (function(a,b,c){for(a+=c=[],b=a.length;(b-=3)>-3;c.unshift(a.substr(b<0?0:b,b<0?-b:3)));return''+c})(1234567890)
</script>
@tsaniel
Copy link

tsaniel commented Jan 20, 2013

c.join(',') could be replaced with ''+c, maybe?

@tsaniel
Copy link

tsaniel commented Jan 20, 2013

save some bytes

function(a,b,c){for(a+=c=[],b=a.length;(b-=3)>-3;c.unshift(a.substr(b<0?0:b,b<0?-b:3)));return''+c}

@atk
Copy link
Author

atk commented Jan 21, 2013

Thanks, @tsaniel. I originally wrote this with german notation (dots instead of commas) in mind, so I didn't catch the possibility for string coercion :-)

@tsaniel
Copy link

tsaniel commented Jan 21, 2013

I think we can use something like for(...;b-=b%3||3;) to get rid of the lengthy calculation inside substr, any idea?

@tsaniel
Copy link

tsaniel commented Jan 22, 2013

And there was another number splitter, but here we use different approach.

@atk
Copy link
Author

atk commented Jan 22, 2013

Since the new github layout, I never find these older gists :-(

@tsaniel
Copy link

tsaniel commented Jan 22, 2013

And not to mention I get no notifications when someone's replying (plus my old notifications gone).

@atk
Copy link
Author

atk commented Jan 23, 2013

That irks me, too.

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