When you have a number that you have to display that’s greater than 10,000, it’s really tempting to hard-code the quantifier so you can shorten the number. Something like `12.6 Thousand`.
That’s all well and good, but we like numbers to grow. What happens when you hit 1 million? You don’t want 1000.1 Thousand
showing up on your site. That’s no fun.
This is where Number_to_Human comes to the rescue. It pretty prints a number in a way that is more readable by us humans. For example, 12790000000
becomes 127.9 Million
. And the best part? It will automatically switch over to 1.0 Billion
when the time is right.
number_to_human(number, options = {})
This helpful helper method gives you lots of customizability too. Things like :precision
, :separator
, :delimiter
, and :units
can all be set by passing in custom values in the options hash.
It kinda just works how you would think too. Which is great.
number_to_human(123) # => "123" number_to_human(1234) # => "1.23 Thousand" number_to_human(12345) # => "12.3 Thousand" number_to_human(1234567) # => "1.23 Million number_to_human(1234567, precision: 1, separator: ',', significant: false) # => "1,2 Million"
Hopefully you can use this to remove hard-coded units from your views.
For full documentation, see the docs.