{{ message }}

Instantly share code, notes, and snippets.

Last active Dec 11, 2015
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 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" ] }
 Decimal markers
Expected value: 12,234,567,890
Actual value:

### tsaniel commented Jan 20, 2013

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

### 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 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 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 commented Jan 22, 2013

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

### atk commented Jan 22, 2013

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