Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a gem to shorten large numbers like Twitter does for followers?

For example:

  • 410589 actually shows up as 410K
  • 4509 actually shows up as 4K
  • 349 actually shows up as 349

I'm looking for a way to shorten very large numbers into a nice to read format.

like image 949
sergserg Avatar asked Feb 01 '26 18:02

sergserg


1 Answers

number_to_human is very close to what you're asking for. It rounds up and down, so 410589 would become "411 K", not sure if that's ok.

You'll need either some I18n mappings, or provide a hash of units to get your suffixes, and a little tweaking 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"
like image 140
smathy Avatar answered Feb 04 '26 10:02

smathy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!