Created
January 22, 2015 00:49
-
-
Save hannesl/200a8e04863213642b43 to your computer and use it in GitHub Desktop.
Format a numeric value with spaces between every third digit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function formatNumber(value) { | |
| var input = value.toString(), | |
| inputParts = input.split('.'), | |
| int = inputParts[0], | |
| decimals = inputParts.length > 1 ? '.' + inputParts[1] : '', | |
| output = []; | |
| while (int.length > 3) { | |
| output.unshift(int.substr(-3)); | |
| int = int.substr(0, int.length - 3); | |
| } | |
| if (int.length > 0) { | |
| output.unshift(int); | |
| } | |
| return output.join(' ') + decimals; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment