Is there a gem to cut down on big numbers like Twitter for followers?

For example:

  • 410589 is actually displayed as 410K
  • 4509 is actually displayed as 4K
  • 349 is actually displayed as 349

I'm looking for a way to condense very large numbers into an easy to read format.

+3


source to share


1 answer


number_to_human

very close to what you are asking. It rounds up and down, so 410589 becomes "411 K", not sure if that's okay.

You will need either some I18n collations or a hash of units to get your suffixes, and a little tweak to get the precision you want, but for example:



opts = { units: { thousand: "K" }, precision: 0, significant: false }

number_to_human 410589, opts  # => "411 K"
number_to_human 4509, opts    # => "5 K"
number_to_human 349, opts     # => "349"

      

+3


source







All Articles