instance method number_to_human_size

Ruby on Rails 2.2.3

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 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, *args)

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

Options

  • :precision - Sets the level of precision (defaults to 1).

  • :separator - Sets the separator between the units (defaults to “.”).

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

Examples

number_to_human_size(123)                                          # => 123 Bytes
number_to_human_size(1234)                                         # => 1.2 KB
number_to_human_size(12345)                                        # => 12.1 KB
number_to_human_size(1234567)                                      # => 1.2 MB
number_to_human_size(1234567890)                                   # => 1.1 GB
number_to_human_size(1234567890123)                                # => 1.1 TB
number_to_human_size(1234567, :precision => 2)                     # => 1.18 MB
number_to_human_size(483989, :precision => 0)                      # => 473 KB
number_to_human_size(1234567, :precision => 2, :separator => ',')  # => 1,18 MB

You can still use number_to_human_size with the old API that accepts the precision as its optional second parameter:

number_to_human_size(1234567, 2)    # => 1.18 MB
number_to_human_size(483989, 0)     # => 473 KB

Parameters

number req
args rest
Source
# File actionpack/lib/action_view/helpers/number_helper.rb, line 249
      def number_to_human_size(number, *args)
        return number.nil? ? nil : pluralize(number.to_i, "Byte") if number.to_i < 1024

        options = args.extract_options!
        options.symbolize_keys!

        defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
        human    = I18n.translate(:'number.human.format', :locale => options[:locale], :raise => true) rescue {}
        defaults = defaults.merge(human)
        storage_units = I18n.translate(:'number.human.storage_units', :locale => options[:locale], :raise => true)

        unless args.empty?
          ActiveSupport::Deprecation.warn('number_to_human_size takes an option hash ' +
            'instead of a separate precision argument.', caller)
          precision = args[0] || defaults[:precision]
        end

        precision ||= (options[:precision] || defaults[:precision])
        separator ||= (options[:separator] || defaults[:separator])
        delimiter ||= (options[:delimiter] || defaults[:delimiter])

        max_exp  = storage_units.size - 1
        number   = Float(number)
        exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
        exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
        number  /= 1024 ** exponent
        unit     = storage_units[exponent]

        begin
          escaped_separator = Regexp.escape(separator)
          number_with_precision(number,
            :precision => precision,
            :separator => separator,
            :delimiter => delimiter
          ).sub(/(\d)(#{escaped_separator}[1-9]*)?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '') + " #{unit}"
        rescue
          number
        end
      end

Defined in actionpack/lib/action_view/helpers/number_helper.rb line 249 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::NumberHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close