Method: ActiveSupport::NumberHelper#number_to_human_size
- Defined in:
- activesupport/lib/active_support/number_helper.rb
#number_to_human_size(number, options = {}) ⇒ Object
Formats number as bytes into a more human-friendly representation. Useful for reporting file sizes to users.
number_to_human_size(123) # => "123 Bytes"
number_to_human_size(1234) # => "1.21 KB"
number_to_human_size(12345) # => "12.1 KB"
number_to_human_size(1234567) # => "1.18 MB"
number_to_human_size(1234567890) # => "1.15 GB"
number_to_human_size(1234567890123) # => "1.12 TB"
number_to_human_size(1234567890123456) # => "1.1 PB"
number_to_human_size(1234567890123456789) # => "1.07 EB"
See #number_to_human if you want to pretty-print a generic number.
Options
:locale-
The locale to use for formatting. Defaults to the current locale.
:precision-
The level of precision. Defaults to 3.
number_to_human_size(123456, precision: 2) # => "120 KB" number_to_human_size(1234567, precision: 2) # => "1.2 MB" :round_mode-
Specifies how rounding is performed. See
BigDecimal.mode. Defaults to:default.number_to_human_size(123456, precision: 2, round_mode: :up) # => "130 KB" :significant-
Whether
:precisionshould be applied to significant digits instead of fractional digits. Defaults to true. :separator-
The decimal separator. Defaults to
".".number_to_human_size(1234567, separator: ",") # => "1,18 MB" :delimiter-
The thousands delimiter. Defaults to
",". :strip_insignificant_zeros-
Whether to remove insignificant zeros after the decimal separator. Defaults to true.
373 374 375 |
# File 'activesupport/lib/active_support/number_helper.rb', line 373 def number_to_human_size(number, = {}) NumberToHumanSizeConverter.convert(number, ) end |