Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot
Created November 25, 2014 12:40
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 guillaumepiot/4e1028076b115ab93439 to your computer and use it in GitHub Desktop.
Save guillaumepiot/4e1028076b115ab93439 to your computer and use it in GitHub Desktop.
JAVASCRIPT - Count number format
# Format count numbers depending on how they are:
# 1-999: 999
# 1000+: 1K
# 1100+: 1.1K
# 10000+: 10K
# 100000+: 100K
# 1000000+: 1M
# 1100000+: 1.1M
window.format_count = (value)->
if value >= 1100000
return (value / 1000000).toFixed(1) + 'M'
if value >= 1000000
return (value / 1000000).toFixed(0) + 'M'
if value >= 100000
return (value / 1000).toFixed(0) + 'K'
if value >= 10000
return (value / 1000).toFixed(0) + 'K'
if value >= 1100
return (value / 1000).toFixed(1) + 'K'
if value >= 1000
return (value / 1000).toFixed(0) + 'K'
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment