Skip to content

Instantly share code, notes, and snippets.

@OrelSokolov
Last active August 27, 2019 06:22
Show Gist options
  • Select an option

  • Save OrelSokolov/e9f936b57d34c8f814a8389217338e3c to your computer and use it in GitHub Desktop.

Select an option

Save OrelSokolov/e9f936b57d34c8f814a8389217338e3c to your computer and use it in GitHub Desktop.
# Something like Rails helper, but with short M instead of Million
def short_number_to_human(n)
postfix = { 1 => "", 10**3 => "K", 10**6 => "M", 10**9 => "B"}
key = postfix.keys.select{|k| n/k.to_f >= 1}.max
"#{(n/key.to_f).round}#{postfix[key]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment