Skip to content

Instantly share code, notes, and snippets.

@mbjordan
Created November 13, 2012 19:42
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 mbjordan/4067916 to your computer and use it in GitHub Desktop.
Save mbjordan/4067916 to your computer and use it in GitHub Desktop.
Style a number
function styleNumber(number, precision) {
if (number.toString().length >= 4) {
var place = 1;
if (precision !== undefined && precision === 1) {
place = 10;
} else if (precision === 2) {
place = 100;
}
number = ((number / 100) / 10) * place;
number = Math.round(number) / place + ' k';
}
return number;
}

styleNumber(number, [precision])

number: A number to style. Either a float or an integer.

precision: The optional number of decimal digits to output.

Example: http://jsfiddle.net/matthewbj/uSFDy/

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