0
@@ -22,7 +22,7 @@ module ActionView
0
# number_to_phone(1235551234, :country_code => 1) # => +1-123-555-1234
0
# number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".")
0
- # => +1.123.555.1234 x 1343
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
@@ -71,14 +71,14 @@ module ActionView
0
def number_to_currency(number, options = {})
0
options = options.symbolize_keys
0
defaults = I18n.translate(:'currency.format', :locale => options[:locale]) || {}
0
precision = options[:precision] || defaults[:precision]
0
unit = options[:unit] || defaults[:unit]
0
separator = options[:separator] || defaults[:separator]
0
delimiter = options[:delimiter] || defaults[:delimiter]
0
format = options[:format] || defaults[:format]
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
@@ -118,49 +118,72 @@ module ActionView
0
- # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You
0
- # can customize the format using optional <em>delimiter</em> and <em>separator</em> parameters.
0
+ # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
0
+ # customize the format in the +options+ hash.
0
- # * <tt>delimiter</tt> - Sets the thousands delimiter (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>:separator</tt> - Sets the separator between the units (defaults to ".").
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(98765432.98, " ", ",")
0
+ # number_with_delimiter(12345678) # => 12,345,678
0
+ # number_with_delimiter(12345678.05) # => 12,345,678.05
0
+ # number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
0
+ # number_with_delimiter(12345678, :seperator => ",") # => 12,345,678
0
+ # number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
0
- def number_with_delimiter(number, delimiter=",", separator=".")
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
+ def number_with_delimiter(number, *args)
0
+ options = args.extract_options!
0
+ options[:delimiter] = args[0] || ","
0
+ options[:separator] = args[1] || "."
0
+ options.reverse_merge!(:delimiter => ",", :separator => ".")
0
parts = number.to_s.split('.')
0
- parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
0
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
0
+ parts.join options[:separator]
0
- # Formats a +number+ with the specified level of +precision+ (e.g., 112.32 has a precision of 2). The default
0
- # level of precision is 3.
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
- # number_with_precision(111.2345) # => 111.235
0
- # number_with_precision(111.2345, 2) # => 111.23
0
- # number_with_precision(13, 5) # => 13.00000
0
- # number_with_precision(389.32314, 0) # => 389
0
- def number_with_precision(number, precision=3)
0
- "%01.#{precision}f" % ((Float(number) * (10 ** precision)).round.to_f / 10 ** precision)
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
+ # 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[:precision] = args[0] || 3
0
+ options.reverse_merge!(:precision => 3)
0
+ "%01.#{options[:precision]}f" %
0
+ ((Float(number) * (10 ** options[:precision])).round.to_f / 10 ** options[:precision])
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
+ # (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
+precision+.
0
+ # precision of 1 using the precision parameter
<tt>:precision</tt>.
0
# number_to_human_size(123) # => 123 Bytes
0
@@ -169,17 +192,28 @@ module ActionView
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
+ # 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) # => 4 MB
0
- def number_to_human_size(size, precision=1)
0
- size = Kernel.Float(size)
0
+ # number_to_human_size(483989, 0) # => 473 KB
0
+ def number_to_human_size(size, *args)
0
+ options = args.extract_options!
0
+ options[:precision] = args[0] || 1
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; "%.#{precision}f KB" % (size / 1.0.kilobyte)
0
- when size < 1.gigabyte; "%.#{precision}f MB" % (size / 1.0.megabyte)
0
- when size < 1.terabyte; "%.#{precision}f GB" % (size / 1.0.gigabyte)
0
- else "%.#{precision}f TB" % (size / 1.0.terabyte)
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(/\. /,' ')