module ActiveSupport::NumberHelper
def number_to_human(number, options = {})
number_to_human(0.01, units: :distance) # => "1 centimeter"
number_to_human(0.1, units: :distance) # => "10 centimeters"
number_to_human(10000000, units: :distance) # => "10000 kilometers"
number_to_human(100000, units: :distance) # => "100 kilometers"
number_to_human(1000, units: :distance) # => "1 kilometer"
number_to_human(100, units: :distance) # => "100 meters"
number_to_human(1, units: :distance) # => "1 meter"
Then it can be specified by name:
other: "kilometers"
one: "kilometer"
thousand:
other: "meters"
one: "meter"
unit:
other: "centimeters"
one: "centimeter"
centi:
distance:
en:
The Hash can also be defined as a scope in an I18n locale. For example:
+:pico+, +:femto+.
fractional units: +:deci+, +:centi+, +:mili+, +:micro+, +:nano+,
+:quadrillion+. Additionally, the following keys are supported for
+:hundred+, +:thousand+, +:million+, +:billion+, +:trillion+,
The following keys are supported for integer units: +:unit+, +:ten+,
number_to_human(10000000, units: { unit: "m", thousand: "km" }) # => "10000 km"
number_to_human(100000, units: { unit: "m", thousand: "km" }) # => "100 km"
number_to_human(1000, units: { unit: "m", thousand: "km" }) # => "1 km"
number_to_human(100, units: { unit: "m", thousand: "km" }) # => "100 m"
number_to_human(1, units: { unit: "m", thousand: "km" }) # => "1 m"
A Hash of custom unit quantifier names.
[+:units+]
"%n %u".
%u represents the quantifier (e.g., "Thousand"). Defaults to
The format of the output. %n represents the number, and
[+:format+]
number_to_human(10.01, strip_insignificant_zeros: false) # => "10.0"
number_to_human(10.01) # => "10"
number_to_human(1000000, strip_insignificant_zeros: false) # => "1.00 Million"
number_to_human(1000000) # => "1 Million"
Defaults to true.
Whether to remove insignificant zeros after the decimal separator.
[+:strip_insignificant_zeros+]
The thousands delimiter. Defaults to ",".
[+:delimiter+]
# => "123,5 Thousand"
number_to_human(123456, precision: 4, separator: ",")
The decimal separator. Defaults to ".".
[+:separator+]
fractional digits. Defaults to true.
Whether +:precision+ should be applied to significant digits instead of
[+:significant+]
# => "130 Thousand"
number_to_human(123456, precision: 2, round_mode: :up)
+:default+.
Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
[+:round_mode+]
number_to_human(123456, precision: 4) # => "123.5 Thousand"
number_to_human(123456, precision: 2) # => "120 Thousand"
The level of precision. Defaults to 3.
[+:precision+]
The locale to use for formatting. Defaults to the current locale.
[+:locale+]
==== Options
See #number_to_human_size if you want to pretty-print a file size.
number_to_human(1234567890123456789) # => "1230 Quadrillion"
number_to_human(1234567890123456) # => "1.23 Quadrillion"
number_to_human(1234567890123) # => "1.23 Trillion"
number_to_human(1234567890) # => "1.23 Billion"
number_to_human(1234567) # => "1.23 Million"
number_to_human(12345) # => "12.3 Thousand"
number_to_human(1234) # => "1.23 Thousand"
number_to_human(123) # => "123"
numbers that can become very large and too hard to read.
Formats +number+ into a more human-friendly representation. Useful for
def number_to_human(number, options = {}) NumberToHumanConverter.convert(number, options) end