Skip to content

Instantly share code, notes, and snippets.

@hannesl
Created January 22, 2015 00:49
Show Gist options
  • Select an option

  • Save hannesl/200a8e04863213642b43 to your computer and use it in GitHub Desktop.

Select an option

Save hannesl/200a8e04863213642b43 to your computer and use it in GitHub Desktop.
Format a numeric value with spaces between every third digit.
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