I've set up a custom locale to get ActiveSupport to use short suffixes when calling number_to_human
. Instead of number_to_human(123456) => '123.4 Thousand'
, it gives me number_to_human(123456) => '123.4k'
.
This all works fine. What doesn't work is that while the default locale would leave smaller numbers alone (i.e. number_to_human(56) => 56
), my custom locale doesn't. I've left the suffixes for units, tens, and hundreds blank, but this results in number_to_human(52) => '5.2'
(i.e. 5.2 tens) or number_to_human(123) => '1.23'
(for 1.23 hundreds).
How do I tell ActiveSupport not to use units, tens, or hundreds at all - to just leave numbers under 1000 alone?
Here's the locale file, if it helps (config/locales/en-ABBREV.yml
):
en-ABBREV:
datetime:
distance_in_words:
x_seconds: '%{count}s'
x_minutes: '%{count}m'
about_x_hours: '%{count}h'
x_hours: '%{count}h'
x_days: '%{count}d'
x_weeks: '%{count}w'
about_x_months: '%{count}mo'
x_months: '%{count}mo'
x_years: '%{count}y'
number:
human:
unit: ''
ten: ''
hundred: ''
thousand: 'k'
million: 'm'
billion: 'b'
trillion: 't'
quadrillion: 'qd'
And my calls to number_to_human
in the view look like this:
number_to_human @posts.count, precision: 1, significant: false, locale: 'en-ABBREV',
units: 'number.human', format: '%n%u'
Looking at the docs of that method I think you can define the unit you want to use like the following. When a key (like tens
) is not included in the units
then that units will just not be used.
number_to_human(
@posts.count,
format: '%n%u',
precision: 1,
significant: false
units: {
thousand: 'k',
million: 'm',
billion: 'b',
trillion: 't',
quadrillion: 'qd'
}
)