539f43c6e3af568c66d24940cac3d276396b3f76
2 module Helpers
#:nodoc:
3 # Provides methods for converting numbers into formatted strings.
4 # Methods are provided for phone numbers, currency, percentage,
5 # precision, positional notation, and file size.
7 # Formats a +number+ into a US phone number (e.g., (555) 123-9876). You can customize the format
8 # in the +options+ hash.
11 # * <tt>:area_code</tt> - Adds parentheses around the area code.
12 # * <tt>:delimiter</tt> - Specifies the delimiter to use (defaults to "-").
13 # * <tt>:extension</tt> - Specifies an extension to add to the end of the
15 # * <tt>:country_code</tt> - Sets the country code for the phone number.
18 # number_to_phone(5551234) # => 555-1234
19 # number_to_phone(1235551234) # => 123-555-1234
20 # number_to_phone(1235551234, :area_code => true) # => (123) 555-1234
21 # number_to_phone(1235551234, :delimiter => " ") # => 123 555 1234
22 # number_to_phone(1235551234, :area_code => true, :extension => 555) # => (123) 555-1234 x 555
23 # number_to_phone(1235551234, :country_code => 1) # => +1-123-555-1234
25 # number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".")
26 # => +1.123.555.1234 x 1343
27 def number_to_phone(number
, options
= {})
28 number
= number
.to_s
.strip
unless number
.nil?
29 options
= options
.symbolize_keys
30 area_code
= options
[:area_code] || nil
31 delimiter
= options
[:delimiter] || "-"
32 extension
= options
[:extension].to_s
.strip
|| nil
33 country_code
= options
[:country_code] || nil
37 str
<< "+#{country_code}#{delimiter}" unless country_code
.blank
?
39 number
.gsub
!(/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/,"(\\1) \\2#{delimiter}\\3")
41 number
.gsub
!(/([0-9]{0,3})([0-9]{3})([0-9]{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
42 number
.starts_with
?('-') ? number
.slice
!(1..-1) : number
44 str
<< " x #{extension}" unless extension
.blank
?
51 # Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
52 # in the +options+ hash.
55 # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
56 # * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$").
57 # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
58 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
59 # * <tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are:
61 # %u The currency unit
65 # number_to_currency(1234567890.50) # => $1,234,567,890.50
66 # number_to_currency(1234567890.506) # => $1,234,567,890.51
67 # number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506
69 # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "")
70 # # => £1234567890,50
71 # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u")
72 # # => 1234567890,50 £
73 def number_to_currency(number
, options
= {})
74 options
.symbolize_keys
!
76 defaults
= I18n
.translate(:'number.format', :locale => options
[:locale], :raise => true) rescue {}
77 currency
= I18n
.translate(:'number.currency.format', :locale => options
[:locale], :raise => true) rescue {}
78 defaults
= defaults
.merge(currency
)
80 precision
= options
[:precision] || defaults
[:precision]
81 unit
= options
[:unit] || defaults
[:unit]
82 separator
= options
[:separator] || defaults
[:separator]
83 delimiter
= options
[:delimiter] || defaults
[:delimiter]
84 format
= options
[:format] || defaults
[:format]
85 separator
= '' if precision
== 0
88 format
.gsub(/%n/, number_with_precision(number
,
89 :precision => precision
,
90 :delimiter => delimiter
,
91 :separator => separator
)
98 # Formats a +number+ as a percentage string (e.g., 65%). You can customize the
99 # format in the +options+ hash.
102 # * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
103 # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
104 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
107 # number_to_percentage(100) # => 100.000%
108 # number_to_percentage(100, :precision => 0) # => 100%
109 # number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000%
110 # number_to_percentage(302.24398923423, :precision => 5) # => 302.24399%
111 def number_to_percentage(number
, options
= {})
112 options
.symbolize_keys
!
114 defaults
= I18n
.translate(:'number.format', :locale => options
[:locale], :raise => true) rescue {}
115 percentage
= I18n
.translate(:'number.percentage.format', :locale => options
[:locale], :raise => true) rescue {}
116 defaults
= defaults
.merge(percentage
)
118 precision
= options
[:precision] || defaults
[:precision]
119 separator
= options
[:separator] || defaults
[:separator]
120 delimiter
= options
[:delimiter] || defaults
[:delimiter]
123 number_with_precision(number
,
124 :precision => precision
,
125 :separator => separator
,
126 :delimiter => delimiter
) + "%"
132 # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
133 # customize the format in the +options+ hash.
136 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
137 # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
140 # number_with_delimiter(12345678) # => 12,345,678
141 # number_with_delimiter(12345678.05) # => 12,345,678.05
142 # number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
143 # number_with_delimiter(12345678, :seperator => ",") # => 12,345,678
144 # number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
147 # You can still use <tt>number_with_delimiter</tt> with the old API that accepts the
148 # +delimiter+ as its optional second and the +separator+ as its
149 # optional third parameter:
150 # number_with_delimiter(12345678, " ") # => 12 345.678
151 # number_with_delimiter(12345678.05, ".", ",") # => 12.345.678,05
152 def number_with_delimiter(number
, *args
)
153 options
= args
.extract_options
!
154 options
.symbolize_keys
!
156 defaults
= I18n
.translate(:'number.format', :locale => options
[:locale], :raise => true) rescue {}
159 ActiveSupport
::Deprecation.warn('number_with_delimiter takes an option hash ' +
160 'instead of separate delimiter and precision arguments.', caller
)
161 delimiter
= args
[0] || defaults
[:delimiter]
162 separator
= args
[1] || defaults
[:separator]
165 delimiter
||= (options
[:delimiter] || defaults
[:delimiter])
166 separator
||= (options
[:separator] || defaults
[:separator])
169 parts
= number
.to_s
.split('.')
170 parts
[0].gsub
!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
171 parts
.join(separator
)
177 # Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
178 # You can customize the format in the +options+ hash.
181 # * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
182 # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
183 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
186 # number_with_precision(111.2345) # => 111.235
187 # number_with_precision(111.2345, :precision => 2) # => 111.23
188 # number_with_precision(13, :precision => 5) # => 13.00000
189 # number_with_precision(389.32314, :precision => 0) # => 389
190 # number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
193 # You can still use <tt>number_with_precision</tt> with the old API that accepts the
194 # +precision+ as its optional second parameter:
195 # number_with_precision(number_with_precision(111.2345, 2) # => 111.23
196 def number_with_precision(number
, *args
)
197 options
= args
.extract_options
!
198 options
.symbolize_keys
!
200 defaults
= I18n
.translate(:'number.format', :locale => options
[:locale], :raise => true) rescue {}
201 precision_defaults
= I18n
.translate(:'number.precision.format', :locale => options
[:locale],
202 :raise => true) rescue {}
203 defaults
= defaults
.merge(precision_defaults
)
206 ActiveSupport
::Deprecation.warn('number_with_precision takes an option hash ' +
207 'instead of a separate precision argument.', caller
)
208 precision
= args
[0] || defaults
[:precision]
211 precision
||= (options
[:precision] || defaults
[:precision])
212 separator
||= (options
[:separator] || defaults
[:separator])
213 delimiter
||= (options
[:delimiter] || defaults
[:delimiter])
216 rounded_number
= (Float(number
) * (10 ** precision
)).round
.to_f
/ 10 ** precision
217 number_with_delimiter("%01.#{precision}f" % rounded_number
,
218 :separator => separator
,
219 :delimiter => delimiter
)
225 STORAGE_UNITS
= [:byte, :kb, :mb, :gb, :tb].freeze
227 # Formats the bytes in +size+ into a more understandable representation
228 # (e.g., giving it 1500 yields 1.5 KB). This method is useful for
229 # reporting file sizes to users. This method returns nil if
230 # +size+ cannot be converted into a number. You can customize the
231 # format in the +options+ hash.
234 # * <tt>:precision</tt> - Sets the level of precision (defaults to 1).
235 # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
236 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
239 # number_to_human_size(123) # => 123 Bytes
240 # number_to_human_size(1234) # => 1.2 KB
241 # number_to_human_size(12345) # => 12.1 KB
242 # number_to_human_size(1234567) # => 1.2 MB
243 # number_to_human_size(1234567890) # => 1.1 GB
244 # number_to_human_size(1234567890123) # => 1.1 TB
245 # number_to_human_size(1234567, :precision => 2) # => 1.18 MB
246 # number_to_human_size(483989, :precision => 0) # => 473 KB
247 # number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB
249 # You can still use <tt>number_to_human_size</tt> with the old API that accepts the
250 # +precision+ as its optional second parameter:
251 # number_to_human_size(1234567, 2) # => 1.18 MB
252 # number_to_human_size(483989, 0) # => 473 KB
253 def number_to_human_size(number
, *args
)
254 return nil if number
.nil?
256 options
= args
.extract_options
!
257 options
.symbolize_keys
!
259 defaults
= I18n
.translate(:'number.format', :locale => options
[:locale], :raise => true) rescue {}
260 human
= I18n
.translate(:'number.human.format', :locale => options
[:locale], :raise => true) rescue {}
261 defaults
= defaults
.merge(human
)
264 ActiveSupport
::Deprecation.warn('number_to_human_size takes an option hash ' +
265 'instead of a separate precision argument.', caller
)
266 precision
= args
[0] || defaults
[:precision]
269 precision
||= (options
[:precision] || defaults
[:precision])
270 separator
||= (options
[:separator] || defaults
[:separator])
271 delimiter
||= (options
[:delimiter] || defaults
[:delimiter])
273 storage_units_format
= I18n
.translate(:'number.human.storage_units.format', :locale => options
[:locale], :raise => true)
275 if number
.to_i
< 1024
276 unit
= I18n
.translate(:'number.human.storage_units.units.byte', :locale => options
[:locale], :count => number
.to_i
, :raise => true)
277 storage_units_format
.gsub(/%n/, number
.to_i
.to_s
).gsub(/%u/, unit
)
279 max_exp
= STORAGE_UNITS
.size
- 1
280 number
= Float(number
)
281 exponent
= (Math
.log(number
) / Math
.log(1024)).to_i
# Convert to base 1024
282 exponent
= max_exp
if exponent
> max_exp
# we need this to avoid overflow for the highest unit
283 number
/= 1024 ** exponent
285 unit_key
= STORAGE_UNITS
[exponent
]
286 unit
= I18n
.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options
[:locale], :count => number
, :raise => true)
289 escaped_separator
= Regexp
.escape(separator
)
290 formatted_number
= number_with_precision(number
,
291 :precision => precision
,
292 :separator => separator
,
293 :delimiter => delimiter
294 ).sub(/(\d)(#{escaped_separator}[1-9]*)?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
295 storage_units_format
.gsub(/%n/, formatted_number
).gsub(/%u/, unit
)