module Padrino::Helpers::NumberHelpers

def number_to_human_size(number, *args)


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

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

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

specified precision:
Zeros after the decimal point are always stripped out, regardless of the

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

==== Examples

:delimiter:: Sets the thousands delimiter (defaults to "").
:separator:: Sets the separator between the units (defaults to ".").
:precision:: Sets the level of precision (defaults to 1).

==== Options

format in the +options+ hash.
+size+ cannot be converted into a number. You can customize the
reporting file sizes to users. This method returns nil if
(e.g., giving it 1500 yields 1.5 KB). This method is useful for
Formats the bytes in +size+ into a more understandable representation
#
def number_to_human_size(number, *args)
  return nil if number.nil?
  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)
  precision ||= (options[:precision] || defaults[:precision])
  separator ||= (options[:separator] || defaults[:separator])
  delimiter ||= (options[:delimiter] || defaults[:delimiter])
  storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
  if number.to_i < 1024
    unit = I18n.translate(:'number.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
    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_key = STORAGE_UNITS[exponent]
    unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)
    begin
      escaped_separator = Regexp.escape(separator)
      formatted_number = number_with_precision(number,
        :precision => precision,
        :separator => separator,
        :delimiter => delimiter
      ).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
      storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
    rescue
      number
    end
  end
end