instance method number_to_human_size

Ruby on Rails 4.0.13

Since v4.0.13

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

number_to_human_size(number, options = {})

Formats the bytes in number into a more understandable representation (e.g., giving it 1500 yields 1.5 KB). This method is useful for reporting file sizes to users. You can customize the format in the options hash.

See number_to_human if you want to pretty-print a generic number.

Options

  • :locale - Sets the locale to be used for formatting (defaults to current locale).

  • :precision - Sets the precision of the number (defaults to 3).

  • :significant - If true, precision will be the # of significant_digits. If false, the # of fractional digits (defaults to true)

  • :separator - Sets the separator between the fractional and integer digits (defaults to “.”).

  • :delimiter - Sets the thousands delimiter (defaults to “”).

  • :strip_insignificant_zeros - If true removes insignificant zeros after the decimal separator (defaults to true)

  • :prefix - If :si formats the number using the SI prefix (defaults to :binary)

Examples

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(1234567, precision: 2)                  # => 1.2 MB
number_to_human_size(483989, precision: 2)                   # => 470 KB
number_to_human_size(1234567, precision: 2, separator: ',')  # => 1,2 MB

Non-significant zeros after the fractional separator are stripped out by default (set :strip_insignificant_zeros to false to change that):

number_to_human_size(1234567890123, precision: 5) # => "1.1229 TB"
number_to_human_size(524288000, precision: 5)     # => "500 MB"

Parameters

number req
options opt = {}
Source
# File activesupport/lib/active_support/number_helper.rb, line 422
    def number_to_human_size(number, options = {})
      options = options.symbolize_keys

      return number unless valid_float?(number)
      number = Float(number)

      defaults = format_options(options[:locale], :human)
      options  = defaults.merge!(options)

      #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
      options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros)

      storage_units_format = translate_number_value_with_default('human.storage_units.format', :locale => options[:locale], :raise => true)

      base = options[:prefix] == :si ? 1000 : 1024

      if number.to_i < base
        unit = translate_number_value_with_default('human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)
        storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit)
      else
        max_exp  = STORAGE_UNITS.size - 1
        exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
        exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
        number  /= base ** exponent

        unit_key = STORAGE_UNITS[exponent]
        unit = translate_number_value_with_default("human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)

        formatted_number = self.number_to_rounded(number, options)
        storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
      end
    end

Defined in activesupport/lib/active_support/number_helper.rb line 422 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::NumberHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close