0
@@ -25,11 +25,11 @@ module ActionView
0
# => +1.123.555.1234 x 1343
0
def number_to_phone(number, options = {})
0
number = number.to_s.strip unless number.nil?
0
- options = options.stringify_keys
0
- area_code = options["area_code"] || nil
0
- delimiter = options["delimiter"] || "-"
0
- extension = options["extension"].to_s.strip || nil
0
- country_code = options["country_code"] || nil
0
+ options = options.symbolize_keys
0
+ area_code = options[:area_code] || nil
0
+ delimiter = options[:delimiter] || "-"
0
+ extension = options[:extension].to_s.strip || nil
0
+ country_code = options[:country_code] || nil
0
@@ -51,10 +51,10 @@ module ActionView
0
# * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
0
- # * <tt>:unit</tt>
- Sets the denomination of the currency (defaults to "$").
0
+ # * <tt>:unit</tt>
- Sets the denomination of the currency (defaults to "$").
0
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
0
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
0
- # * <tt>:format</tt>
- Sets the format of the output string (defaults to "%u%n"). The field types are:
0
+ # * <tt>:format</tt>
- Sets the format of the output string (defaults to "%u%n"). The field types are:
0
@@ -69,8 +69,11 @@ module ActionView
0
# number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u")
0
# # => 1234567890,50 £
0
def number_to_currency(number, options = {})
0
- options = options.symbolize_keys
0
- defaults = I18n.translate(:'currency.format', :locale => options[:locale]) || {}
0
+ options.symbolize_keys!
0
+ defaults, currency = I18n.translate([:'number.format', :'number.currency.format'],
0
+ :locale => options[:locale]) || [{},{}]
0
+ defaults = defaults.merge(currency)
0
precision = options[:precision] || defaults[:precision]
0
unit = options[:unit] || defaults[:unit]
0
@@ -80,8 +83,11 @@ module ActionView
0
separator = '' if precision == 0
0
- parts = number_with_precision(number, precision).split('.')
0
- format.gsub(/%n/, number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s).gsub(/%u/, unit)
0
+ format.gsub(/%n/, number_with_precision(number,
0
+ :precision => precision,
0
+ :delimiter => delimiter,
0
+ :separator => separator)
0
@@ -93,26 +99,29 @@ module ActionView
0
# * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
0
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
0
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
0
- # number_to_percentage(100) # => 100.000%
0
- # number_to_percentage(100, :precision => 0) # => 100%
0
- # number_to_percentage(302.24398923423, :precision => 5)
0
+ # number_to_percentage(100) # => 100.000%
0
+ # number_to_percentage(100, :precision => 0) # => 100%
0
+ # number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000%
0
+ # number_to_percentage(302.24398923423, :precision => 5) # => 302.24399%
0
def number_to_percentage(number, options = {})
0
- options = options.stringify_keys
0
- precision = options["precision"] || 3
0
- separator = options["separator"] || "."
0
+ options.symbolize_keys!
0
+ defaults, percentage = I18n.translate([:'number.format', :'number.percentage.format'],
0
+ :locale => options[:locale]) || [{},{}]
0
+ defaults = defaults.merge(percentage)
0
+ precision = options[:precision] || defaults[:precision]
0
+ separator = options[:separator] || defaults[:separator]
0
+ delimiter = options[:delimiter] || defaults[:delimiter]
0
- number = number_with_precision(number, precision)
0
- parts = number.split('.')
0
- parts[0] + separator + parts[1].to_s + "%"
0
+ number_with_precision(number,
0
+ :precision => precision,
0
+ :separator => separator,
0
+ :delimiter => delimiter) + "%"
0
@@ -136,87 +145,140 @@ module ActionView
0
# You can still use <tt>number_with_delimiter</tt> with the old API that accepts the
0
# +delimiter+ as its optional second and the +separator+ as its
0
# optional third parameter:
0
- # number_with_delimiter(12345678, " ") # => 12 345.678
0
- # number_with_delimiter(12345678.05, ".", ",") # => 12.345.678,05
0
+ # number_with_delimiter(12345678, " ") # => 12 345.678
0
+ # number_with_delimiter(12345678.05, ".", ",") # => 12.345.678,05
0
def number_with_delimiter(number, *args)
0
options = args.extract_options!
0
+ options.symbolize_keys!
0
+ defaults = I18n.translate(:'number.format', :locale => options[:locale]) || {}
0
- options[:delimiter] = args[0] || ","
0
- options[:separator] = args[1] || "."
0
+ ActiveSupport::Deprecation.warn('number_with_delimiter takes an option hash ' +
0
+ 'instead of separate delimiter and precision arguments.', caller)
0
+ delimiter = args[0] || defaults[:delimiter]
0
+ separator = args[1] || defaults[:separator]
0
- options.reverse_merge!(:delimiter => ",", :separator => ".")
0
+ delimiter ||= (options[:delimiter] || defaults[:delimiter])
0
+ separator ||= (options[:separator] || defaults[:separator])
0
parts = number.to_s.split('.')
0
- parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
0
- parts.join options[:separator]
0
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
0
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
0
- # The default level of precision is 3.
0
+ # You can customize the format in the +options+ hash.
0
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
0
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
0
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
0
# number_with_precision(111.2345) # => 111.235
0
# number_with_precision(111.2345, :precision => 2) # => 111.23
0
# number_with_precision(13, :precision => 5) # => 13.00000
0
# number_with_precision(389.32314, :precision => 0) # => 389
0
+ # number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
0
# You can still use <tt>number_with_precision</tt> with the old API that accepts the
0
# +precision+ as its optional second parameter:
0
# number_with_precision(number_with_precision(111.2345, 2) # => 111.23
0
def number_with_precision(number, *args)
0
options = args.extract_options!
0
+ options.symbolize_keys!
0
+ defaults, precision_defaults = I18n.translate([:'number.format', :'number.precision.format'],
0
+ :locale => options[:locale]) || [{},{}]
0
+ defaults = defaults.merge(precision_defaults)
0
- options[:precision] = args[0] || 3
0
+ ActiveSupport::Deprecation.warn('number_with_precision takes an option hash ' +
0
+ 'instead of a separate precision argument.', caller)
0
+ precision = args[0] || defaults[:precision]
0
- options.reverse_merge!(:precision => 3)
0
- "%01.#{options[:precision]}f" %
0
- ((Float(number) * (10 ** options[:precision])).round.to_f / 10 ** options[:precision])
0
+ precision ||= (options[:precision] || defaults[:precision])
0
+ separator ||= (options[:separator] || defaults[:separator])
0
+ delimiter ||= (options[:delimiter] || defaults[:delimiter])
0
+ rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision
0
+ number_with_delimiter("%01.#{precision}f" % rounded_number,
0
+ :separator => separator,
0
+ :delimiter => delimiter)
0
+ STORAGE_UNITS = %w( Bytes KB MB GB TB ).freeze
0
# Formats the bytes in +size+ into a more understandable representation
0
# (e.g., giving it 1500 yields 1.5 KB). This method is useful for
0
# reporting file sizes to users. This method returns nil if
0
- # +size+ cannot be converted into a number. You can change the default
0
- # precision of 1 using the precision parameter <tt>:precision</tt>.
0
+ # +size+ cannot be converted into a number. You can customize the
0
+ # format in the +options+ hash.
0
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 1).
0
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
0
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
0
- # number_to_human_size(123) # => 123 Bytes
0
- # number_to_human_size(1234) # => 1.2 KB
0
- # number_to_human_size(12345) # => 12.1 KB
0
- # number_to_human_size(1234567) # => 1.2 MB
0
- # number_to_human_size(1234567890) # => 1.1 GB
0
- # number_to_human_size(1234567890123) # => 1.1 TB
0
- # number_to_human_size(1234567, :precision => 2) # => 1.18 MB
0
- # number_to_human_size(483989, :precision => 0) # => 473 KB
0
+ # number_to_human_size(123) # => 123 Bytes
0
+ # number_to_human_size(1234) # => 1.2 KB
0
+ # number_to_human_size(12345) # => 12.1 KB
0
+ # number_to_human_size(1234567) # => 1.2 MB
0
+ # number_to_human_size(1234567890) # => 1.1 GB
0
+ # number_to_human_size(1234567890123) # => 1.1 TB
0
+ # number_to_human_size(1234567, :precision => 2) # => 1.18 MB
0
+ # number_to_human_size(483989, :precision => 0) # => 473 KB
0
+ # number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB
0
# You can still use <tt>number_to_human_size</tt> with the old API that accepts the
0
# +precision+ as its optional second parameter:
0
# number_to_human_size(1234567, 2) # => 1.18 MB
0
# number_to_human_size(483989, 0) # => 473 KB
0
- def number_to_human_size(size, *args)
0
+ def number_to_human_size(number, *args)
0
+ return number.nil? ? nil : pluralize(number.to_i, "Byte") if number.to_i < 1024
0
options = args.extract_options!
0
+ options.symbolize_keys!
0
+ defaults, human = I18n.translate([:'number.format', :'number.human.format'],
0
+ :locale => options[:locale]) || [{},{}]
0
+ defaults = defaults.merge(human)
0
- options[:precision] = args[0] || 1
0
+ ActiveSupport::Deprecation.warn('number_to_human_size takes an option hash ' +
0
+ 'instead of a separate precision argument.', caller)
0
+ precision = args[0] || defaults[:precision]
0
- options.reverse_merge!(:precision => 1)
0
- when size.to_i == 1; "1 Byte"
0
- when size < 1.kilobyte; "%d Bytes" % size
0
- when size < 1.megabyte; "%.#{options[:precision]}f KB" % (size / 1.0.kilobyte)
0
- when size < 1.gigabyte; "%.#{options[:precision]}f MB" % (size / 1.0.megabyte)
0
- when size < 1.terabyte; "%.#{options[:precision]}f GB" % (size / 1.0.gigabyte)
0
- else "%.#{options[:precision]}f TB" % (size / 1.0.terabyte)
0
- end.sub(/([0-9]\.\d*?)0+ /, '\1 ' ).sub(/\. /,' ')
0
+ precision ||= (options[:precision] || defaults[:precision])
0
+ separator ||= (options[:separator] || defaults[:separator])
0
+ delimiter ||= (options[:delimiter] || defaults[:delimiter])
0
+ max_exp = STORAGE_UNITS.size - 1
0
+ number = Float(number)
0
+ exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
0
+ exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
0
+ number /= 1024 ** exponent
0
+ unit = STORAGE_UNITS[exponent]
0
+ number_with_precision(number,
0
+ :precision => precision,
0
+ :separator => separator,
0
+ :delimiter => delimiter
0
+ ).sub(/(\d)(#{Regexp.escape(separator)}[1-9]*)?0+\z/, '\1') + " #{unit}"