Skip to content
This repository
Newer
Older
100644 513 lines (459 sloc) 28.089 kb
d352e0df » fxn
2011-07-23 checked all .rb files in the project tree for missing magic comments,…
1 # encoding: utf-8
2
31319b47 » josh
2009-10-03 NumberHelper depends on big decimal extensions
3 require 'active_support/core_ext/big_decimal/conversions'
76f024ac » fxn
2010-03-28 adds missing requires for Object#blank? and Object#present?
4 require 'active_support/core_ext/object/blank'
5f607351 » spastorino
2010-10-18 Add missing require
5 require 'active_support/core_ext/string/output_safety'
b2d6fdae » jeremy
2009-04-22 Fix tests on 1.9
6
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
7 module ActionView
1a0dc267 » rizwanreza
2010-06-21 Action View is now titled.
8 # = Action View Number Helpers
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
9 module Helpers #:nodoc:
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
10
9ffb6328 » Marcel Molina
2007-12-05 Enhance documentation coverage for fragment caching. Closes #7315 [bs…
11 # Provides methods for converting numbers into formatted strings.
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
12 # Methods are provided for phone numbers, currency, percentage,
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
13 # precision, positional notation, file size and pretty printing.
14 #
15 # Most methods expect a +number+ argument, and will return it
16 # unchanged if can't be converted into a valid number.
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
17 module NumberHelper
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
18
c4d977fb » dontangg
2010-11-02 Added :negative_format option to number_to_currency function [#5894 s…
19 DEFAULT_CURRENCY_VALUES = { :format => "%u%n", :negative_format => "-%u%n", :unit => "$", :separator => ".", :delimiter => ",",
c7e67779 » spastorino
2010-05-16 Added default currency values to NumberHelper and pass them to I18n.t…
20 :precision => 2, :significant => false, :strip_insignificant_zeros => false }
21
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
22 # Raised when argument +number+ param given to the helpers is invalid and
23 # the option :raise is set to +true+.
24 class InvalidNumberError < StandardError
25 attr_accessor :number
26 def initialize(number)
27 @number = number
28 end
29 end
30
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
31 # Formats a +number+ into a US phone number (e.g., (555) 123-9876). You can customize the format
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
32 # in the +options+ hash.
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
33 #
34 # ==== Options
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
35 #
36 # * <tt>:area_code</tt> - Adds parentheses around the area code.
37 # * <tt>:delimiter</tt> - Specifies the delimiter to use (defaults to "-").
38 # * <tt>:extension</tt> - Specifies an extension to add to the end of the
39 # generated number.
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
40 # * <tt>:country_code</tt> - Sets the country code for the phone number.
6fca9315 » vijaydev
2012-01-04 copy edits
41 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
42 #
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
43 # ==== Examples
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
44 #
1ab2ff58 » rpheath
2009-03-08 Fixed number_to_phone to work with 7 digit numbers [#2176 state:resol…
45 # number_to_phone(5551234) # => 555-1234
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
46 # number_to_phone("5551234") # => 555-1234
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
47 # number_to_phone(1235551234) # => 123-555-1234
48 # number_to_phone(1235551234, :area_code => true) # => (123) 555-1234
49 # number_to_phone(1235551234, :delimiter => " ") # => 123 555 1234
50 # number_to_phone(1235551234, :area_code => true, :extension => 555) # => (123) 555-1234 x 555
51 # number_to_phone(1235551234, :country_code => 1) # => +1-123-555-1234
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
52 # number_to_phone("123a456") # => 123a456
53 #
6fca9315 » vijaydev
2012-01-04 copy edits
54 # number_to_phone("1234a567", :raise => true) # => InvalidNumberError
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
55 #
2af36bbb » dhh
2007-12-05 Fix typos (closes #10378)
56 # number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".")
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
57 # # => +1.123.555.1234 x 1343
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
58 def number_to_phone(number, options = {})
98d3c41c » tenderlove
2010-10-18 implicit return of nil
59 return unless number
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
60 options = options.symbolize_keys
8da026cb » xaviershay
2010-02-17 Remove some of the blank rescues from number helper. This makes the c…
61
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
62 parse_float(number, true) if options[:raise]
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
63
8da026cb » xaviershay
2010-02-17 Remove some of the blank rescues from number helper. This makes the c…
64 number = number.to_s.strip
dfb3ff66 » spastorino
2010-10-18 This or login doesn't make sense
65 area_code = options[:area_code]
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
66 delimiter = options[:delimiter] || "-"
1a0f72d0 » spastorino
2010-10-18 Why strip only the extension, this can be done by the user
67 extension = options[:extension]
dfb3ff66 » spastorino
2010-10-18 This or login doesn't make sense
68 country_code = options[:country_code]
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
69
31ac1db5 » spastorino
2010-10-18 Tidy up a bit this code
70 if area_code
9c219bf3 » tenderlove
2010-10-18 use regex escape sequences to shorten stuff up
71 number.gsub!(/(\d{1,3})(\d{3})(\d{4}$)/,"(\\1) \\2#{delimiter}\\3")
8da026cb » xaviershay
2010-02-17 Remove some of the blank rescues from number helper. This makes the c…
72 else
9c219bf3 » tenderlove
2010-10-18 use regex escape sequences to shorten stuff up
73 number.gsub!(/(\d{0,3})(\d{3})(\d{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
74 number.slice!(0, 1) if number.start_with?(delimiter) && !delimiter.blank?
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
75 end
31ac1db5 » spastorino
2010-10-18 Tidy up a bit this code
76
332c3e14 » lest
2012-04-09 no need in temporary array in number_to_phone helper
77 str = ''
31ac1db5 » spastorino
2010-10-18 Tidy up a bit this code
78 str << "+#{country_code}#{delimiter}" unless country_code.blank?
79 str << number
8da026cb » xaviershay
2010-02-17 Remove some of the blank rescues from number helper. This makes the c…
80 str << " x #{extension}" unless extension.blank?
332c3e14 » lest
2012-04-09 no need in temporary array in number_to_phone helper
81 ERB::Util.html_escape(str)
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
82 end
83
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
84 # Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
85 # in the +options+ hash.
86 #
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
87 # ==== Options
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
88 #
c4d977fb » dontangg
2010-11-02 Added :negative_format option to number_to_currency function [#5894 s…
89 # * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
90 # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
91 # * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$").
92 # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
93 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
208fb292 » fxn
2010-11-03 made a pass to the docs of :negative_format
94 # * <tt>:format</tt> - Sets the format for non-negative numbers (defaults to "%u%n").
95 # Fields are <tt>%u</tt> for the currency, and <tt>%n</tt>
96 # for the number.
97 # * <tt>:negative_format</tt> - Sets the format for negative numbers (defaults to prepending
98 # an hyphen to the formatted number given by <tt>:format</tt>).
99 # Accepts the same fields than <tt>:format</tt>, except
100 # <tt>%n</tt> is here the absolute value of the number.
6fca9315 » vijaydev
2012-01-04 copy edits
101 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
102 #
103 # ==== Examples
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
104 #
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
105 # number_to_currency(1234567890.50) # => $1,234,567,890.50
106 # number_to_currency(1234567890.506) # => $1,234,567,890.51
107 # number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506
983a2dcc » avakhov
2011-11-10 Cosmetic fix in number_to_currency docs
108 # number_to_currency(1234567890.506, :locale => :fr) # => 1 234 567 890,51 €
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
109 # number_to_currency("123a456") # => $123a456
110 #
6fca9315 » vijaydev
2012-01-04 copy edits
111 # number_to_currency("123a456", :raise => true) # => InvalidNumberError
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
112 #
7f98b544 » mrduncan
2011-04-15 Negative format example should use a negative number
113 # number_to_currency(-1234567890.50, :negative_format => "(%u%n)")
983a2dcc » avakhov
2011-11-10 Cosmetic fix in number_to_currency docs
114 # # => ($1,234,567,890.50)
2823a56f » jeremy
2007-03-13 Deprecation: remove deprecated human_size helper alias. Use number_to…
115 # number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "")
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
116 # # => &pound;1234567890,50
3c0fd445 » dhh
2008-03-17 Added :format option to NumberHelper#number_to_currency to enable bet…
117 # number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
118 # # => 1234567890,50 &pound;
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
119 def number_to_currency(number, options = {})
a0a5a2a2 » tenderlove
2010-10-18 implicitly return nil if there is no number
120 return unless number
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
121 options = options.symbolize_keys
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
122
977f6b49 » rafaelfranca
2012-01-20 Refactor translations retrieval
123 currency = translations_for('currency', options[:locale])
6724c8c8 » kuroda
2012-01-05 Infer currency negative format from positive one.
124 currency[:negative_format] ||= "-" + currency[:format] if currency[:format]
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
125
977f6b49 » rafaelfranca
2012-01-20 Refactor translations retrieval
126 defaults = DEFAULT_CURRENCY_VALUES.merge(defaults_translations(options[:locale])).merge!(currency)
c4d977fb » dontangg
2010-11-02 Added :negative_format option to number_to_currency function [#5894 s…
127 defaults[:negative_format] = "-" + options[:format] if options[:format]
c7e67779 » spastorino
2010-05-16 Added default currency values to NumberHelper and pass them to I18n.t…
128 options = defaults.merge!(options)
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
129
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
130 unit = options.delete(:unit)
131 format = options.delete(:format)
132
c4d977fb » dontangg
2010-11-02 Added :negative_format option to number_to_currency function [#5894 s…
133 if number.to_f < 0
134 format = options.delete(:negative_format)
135 number = number.respond_to?("abs") ? number.abs : number.sub(/^-/, '')
136 end
137
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
138 begin
139 value = number_with_precision(number, options.merge(:raise => true))
2d8396fc » jurriaan
2012-04-03 Updated/changed useless tr/gsubs
140 format.gsub('%n', value).gsub('%u', unit).html_safe
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
141 rescue InvalidNumberError => e
142 if options[:raise]
143 raise
144 else
2d8396fc » jurriaan
2012-04-03 Updated/changed useless tr/gsubs
145 formatted_number = format.gsub('%n', e.number).gsub('%u', unit)
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
146 e.number.to_s.html_safe? ? formatted_number.html_safe : formatted_number
147 end
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
148 end
149 end
150
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
151 # Formats a +number+ as a percentage string (e.g., 65%). You can customize the format in the +options+ hash.
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
152 #
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
153 # ==== Options
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
154 #
155 # * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current
156 # locale).
157 # * <tt>:precision</tt> - Sets the precision of the number (defaults to 3).
158 # * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits. If +false+,
159 # the # of fractional digits (defaults to +false+).
160 # * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults
161 # to ".").
162 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
163 # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator
164 # (defaults to +false+).
c0a3fe3c » Rodrigo Flores
2012-01-19 Added format to percentage
165 # * <tt>:format</tt> - Specifies the format of the percentage string
166 # The number field is <tt>%n</tt> (defaults to "%n%").
6fca9315 » vijaydev
2012-01-04 copy edits
167 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
168 #
169 # ==== Examples
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
170 #
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
171 # number_to_percentage(100) # => 100.000%
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
172 # number_to_percentage("98") # => 98.000%
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
173 # number_to_percentage(100, :precision => 0) # => 100%
174 # number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000%
175 # number_to_percentage(302.24398923423, :precision => 5) # => 302.24399%
5a745575 » basti
2010-06-11 Updated all number helpers with :locale option.
176 # number_to_percentage(1000, :locale => :fr) # => 1 000,000%
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
177 # number_to_percentage("98a") # => 98a%
c0a3fe3c » Rodrigo Flores
2012-01-19 Added format to percentage
178 # number_to_percentage(100, :format => "%n %") # => 100 %
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
179 #
6fca9315 » vijaydev
2012-01-04 copy edits
180 # number_to_percentage("98a", :raise => true) # => InvalidNumberError
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
181 def number_to_percentage(number, options = {})
a0a5a2a2 » tenderlove
2010-10-18 implicitly return nil if there is no number
182 return unless number
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
183 options = options.symbolize_keys
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
184
fb22f93b » carlosantoniodasilva
2012-01-21 Improve getting translations for number helpers
185 defaults = format_translations('percentage', options[:locale])
186 options = defaults.merge!(options)
2823a56f » jeremy
2007-03-13 Deprecation: remove deprecated human_size helper alias. Use number_to…
187
c0a3fe3c » Rodrigo Flores
2012-01-19 Added format to percentage
188 format = options[:format] || "%n%"
189
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
190 begin
c0a3fe3c » Rodrigo Flores
2012-01-19 Added format to percentage
191 value = number_with_precision(number, options.merge(:raise => true))
192 format.gsub(/%n/, value).html_safe
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
193 rescue InvalidNumberError => e
194 if options[:raise]
195 raise
196 else
c753e3af » carlosantoniodasilva
2012-01-21 Refactor percentage helper
197 formatted_number = format.gsub(/%n/, e.number)
198 e.number.to_s.html_safe? ? formatted_number.html_safe : formatted_number
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
199 end
200 end
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
201 end
202
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
203 # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
204 # customize the format in the +options+ hash.
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
205 #
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
206 # ==== Options
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
207 #
5a745575 » basti
2010-06-11 Updated all number helpers with :locale option.
208 # * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
209 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
210 # * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to ".").
6fca9315 » vijaydev
2012-01-04 copy edits
211 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
212 #
213 # ==== Examples
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
214 #
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
215 # number_with_delimiter(12345678) # => 12,345,678
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
216 # number_with_delimiter("123456") # => 123,456
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
217 # number_with_delimiter(12345678.05) # => 12,345,678.05
218 # number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
5a2db4c7 » vijaydev
2011-11-18 put back the removed eg
219 # number_with_delimiter(12345678, :delimiter => ",") # => 12,345,678
fe88745d » nilbus
2011-11-17 Fix a number_with_delimiter :separator example
220 # number_with_delimiter(12345678.05, :separator => " ") # => 12,345,678 05
5a745575 » basti
2010-06-11 Updated all number helpers with :locale option.
221 # number_with_delimiter(12345678.05, :locale => :fr) # => 12 345 678,05
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
222 # number_with_delimiter("112a") # => 112a
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
223 # number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
224 # # => 98 765 432,98
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
225 #
226 # number_with_delimiter("112a", :raise => true) # => raise InvalidNumberError
da845275 » strzalek
2010-07-20 Removed deprecated APIs in text and number helpers [#5156 state:resol…
227 def number_with_delimiter(number, options = {})
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
228 options = options.symbolize_keys
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
229
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
230 parse_float(number, options[:raise]) or return number
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
231
fb22f93b » carlosantoniodasilva
2012-01-21 Improve getting translations for number helpers
232 options = defaults_translations(options[:locale]).merge(options)
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
233
0bcbcdfd » pixeltrix
2011-06-10 Ensure number helpers can handle HTML safe strings - closes #1597.
234 parts = number.to_s.to_str.split('.')
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
235 parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
0f848dd5 » lest
2012-04-26 use safe_join in number helper
236 safe_join(parts, options[:separator])
4bdb25da » dhh
2005-04-07 Update to the helper to abstract delimeting a number #1015
237 end
2823a56f » jeremy
2007-03-13 Deprecation: remove deprecated human_size helper alias. Use number_to…
238
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
239 # Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision
240 # of 2 if +:significant+ is +false+, and 5 if +:significant+ is +true+).
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
241 # You can customize the format in the +options+ hash.
242 #
243 # ==== Options
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
244 # * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
245 # * <tt>:precision</tt> - Sets the precision of the number (defaults to 3).
246 # * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits. If +false+,
247 # the # of fractional digits (defaults to +false+).
248 # * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults
249 # to ".").
250 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
251 # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator
252 # (defaults to +false+).
83e891cf » Jonathan Roes
2012-01-20 document `:raise` option support for several helpers [ci skip]
253 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
254 #
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
255 # ==== Examples
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
256 # number_with_precision(111.2345) # => 111.235
257 # number_with_precision(111.2345, :precision => 2) # => 111.23
258 # number_with_precision(13, :precision => 5) # => 13.00000
259 # number_with_precision(389.32314, :precision => 0) # => 389
260 # number_with_precision(111.2345, :significant => true) # => 111
261 # number_with_precision(111.2345, :precision => 1, :significant => true) # => 100
262 # number_with_precision(13, :precision => 5, :significant => true) # => 13.000
5a745575 » basti
2010-06-11 Updated all number helpers with :locale option.
263 # number_with_precision(111.234, :locale => :fr) # => 111,234
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
264 #
004042c0 » mrduncan
2011-04-15 Fixing missing colon on symbol in example
265 # number_with_precision(13, :precision => 5, :significant => true, :strip_insignificant_zeros => true)
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
266 # # => 13
24b1fd21 » elpic
2012-01-02 improve doc in number helper options
267 #
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
268 # number_with_precision(389.32314, :precision => 4, :significant => true) # => 389.3
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
269 # number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
270 # # => 1.111,23
da845275 » strzalek
2010-07-20 Removed deprecated APIs in text and number helpers [#5156 state:resol…
271 def number_with_precision(number, options = {})
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
272 options = options.symbolize_keys
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
273
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
274 number = (parse_float(number, options[:raise]) or return number)
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
275
fb22f93b » carlosantoniodasilva
2012-01-21 Improve getting translations for number helpers
276 defaults = format_translations('precision', options[:locale])
277 options = defaults.merge!(options)
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
278
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
279 precision = options.delete :precision
280 significant = options.delete :significant
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
281 strip_insignificant_zeros = options.delete :strip_insignificant_zeros
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
282
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
283 if significant and precision > 0
81e69332 » bernardo
2010-04-01 Fix error in number_with_precision with :significant option and zero …
284 if number == 0
285 digits, rounded_number = 1, 0
286 else
e20012b6 » spastorino
2010-09-05 Make number_to_human and number_with_precision work with negatives
287 digits = (Math.log10(number.abs) + 1).floor
f71809f2 » dontangg
2010-12-17 Fixed number_with_precision rounding error [#6182 state:resolved]
288 rounded_number = (BigDecimal.new(number.to_s) / BigDecimal.new((10 ** (digits - precision)).to_f.to_s)).round.to_f * 10 ** (digits - precision)
289 digits = (Math.log10(rounded_number.abs) + 1).floor # After rounding, the number of digits may have changed
81e69332 » bernardo
2010-04-01 Fix error in number_with_precision with :significant option and zero …
290 end
132f3e2d » tenderlove
2010-10-18 use -= in place of the expanded form
291 precision -= digits
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
292 precision = precision > 0 ? precision : 0 #don't let it be negative
293 else
f71809f2 » dontangg
2010-12-17 Fixed number_with_precision rounding error [#6182 state:resolved]
294 rounded_number = BigDecimal.new(number.to_s).round(precision).to_f
7051a234 » tjmcewan
2012-03-27 change number_with_precision to not return negative zeros
295 rounded_number = rounded_number.zero? ? rounded_number.abs : rounded_number #prevent showing negative zeros
8da026cb » xaviershay
2010-02-17 Remove some of the blank rescues from number helper. This makes the c…
296 end
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
297 formatted_number = number_with_delimiter("%01.#{precision}f" % rounded_number, options)
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
298 if strip_insignificant_zeros
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
299 escaped_separator = Regexp.escape(options[:separator])
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
300 formatted_number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '').html_safe
8da026cb » xaviershay
2010-02-17 Remove some of the blank rescues from number helper. This makes the c…
301 else
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
302 formatted_number
dc66469e » clemens
2008-08-04 Fixed i18n bulk translate issues in NumberHelper
303 end
1c71a5d2 » dhh
2006-10-22 Upgraded NumberHelper with number_to_phone support international form…
304 end
2823a56f » jeremy
2007-03-13 Deprecation: remove deprecated human_size helper alias. Use number_to…
305
17db28f1 » yaroslav
2009-01-01 Improve i18n support for number_to_human_size helper:
306 STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb].freeze
307
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
308 # Formats the bytes in +number+ into a more understandable representation
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
309 # (e.g., giving it 1500 yields 1.5 KB). This method is useful for
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
310 # reporting file sizes to users. You can customize the
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
311 # format in the +options+ hash.
312 #
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
313 # See <tt>number_to_human</tt> if you want to pretty-print a generic number.
314 #
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
315 # ==== Options
5a745575 » basti
2010-06-11 Updated all number helpers with :locale option.
316 # * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
317 # * <tt>:precision</tt> - Sets the precision of the number (defaults to 3).
318 # * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits. If +false+, the # of fractional digits (defaults to +true+)
319 # * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to ".").
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
320 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
321 # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator (defaults to +true+)
07bbaaa3 » dcrec1
2011-04-27 added a :prefix option to number_to_human_size
322 # * <tt>:prefix</tt> - If +:si+ formats the number using the SI prefix (defaults to :binary)
83e891cf » Jonathan Roes
2012-01-20 document `:raise` option support for several helpers [ci skip]
323 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
b00e6a98 » dhh
2007-06-23 Massive documentation update for all helpers (closes #8223, #8177, #8175
324 # ==== Examples
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
325 # number_to_human_size(123) # => 123 Bytes
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
326 # number_to_human_size(1234) # => 1.21 KB
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
327 # number_to_human_size(12345) # => 12.1 KB
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
328 # number_to_human_size(1234567) # => 1.18 MB
329 # number_to_human_size(1234567890) # => 1.15 GB
330 # number_to_human_size(1234567890123) # => 1.12 TB
331 # number_to_human_size(1234567, :precision => 2) # => 1.2 MB
332 # number_to_human_size(483989, :precision => 2) # => 470 KB
333 # number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,2 MB
0f43de64 » clemens
2008-07-21 Refactored NumberHelper API to accept arguments as an options hash [#666
334 #
ccf9577a » dolzenko
2010-06-11 Fix a bunch of minor spelling mistakes
335 # Non-significant zeros after the fractional separator are stripped out by default (set
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
336 # <tt>:strip_insignificant_zeros</tt> to +false+ to change that):
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
337 # number_to_human_size(1234567890123, :precision => 5) # => "1.1229 TB"
e8afe4e1 » mrduncan
2011-04-15 Making spacing consistent
338 # number_to_human_size(524288000, :precision => 5) # => "500 MB"
da845275 » strzalek
2010-07-20 Removed deprecated APIs in text and number helpers [#5156 state:resol…
339 def number_to_human_size(number, options = {})
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
340 options = options.symbolize_keys
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
341
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
342 number = (parse_float(number, options[:raise]) or return number)
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
343
fb22f93b » carlosantoniodasilva
2012-01-21 Improve getting translations for number helpers
344 defaults = format_translations('human', options[:locale])
345 options = defaults.merge!(options)
b451de0d » spastorino
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
346
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
347 #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
348 options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros)
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
349
17db28f1 » yaroslav
2009-01-01 Improve i18n support for number_to_human_size helper:
350 storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
c2c8ef57 » guilleiguaran
2011-06-05 Remove trailing white-spaces
351
07bbaaa3 » dcrec1
2011-04-27 added a :prefix option to number_to_human_size
352 base = options[:prefix] == :si ? 1000 : 1024
fea7771d » clemens
2008-07-29 Updated NumberHelper: Full i18n support (except number_to_phone), con…
353
07bbaaa3 » dcrec1
2011-04-27 added a :prefix option to number_to_human_size
354 if number.to_i < base
17db28f1 » yaroslav
2009-01-01 Improve i18n support for number_to_human_size helper:
355 unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
356 storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit).html_safe
17db28f1 » yaroslav
2009-01-01 Improve i18n support for number_to_human_size helper:
357 else
358 max_exp = STORAGE_UNITS.size - 1
07bbaaa3 » dcrec1
2011-04-27 added a :prefix option to number_to_human_size
359 exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
17db28f1 » yaroslav
2009-01-01 Improve i18n support for number_to_human_size helper:
360 exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
07bbaaa3 » dcrec1
2011-04-27 added a :prefix option to number_to_human_size
361 number /= base ** exponent
17db28f1 » yaroslav
2009-01-01 Improve i18n support for number_to_human_size helper:
362
363 unit_key = STORAGE_UNITS[exponent]
364 unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)
365
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
366 formatted_number = number_with_precision(number, options)
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
367 storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit).html_safe
dc66469e » clemens
2008-08-04 Fixed i18n bulk translate issues in NumberHelper
368 end
7140f653 » dhh
2005-04-12 Moved TextHelper#human_size to NumberHelper#number_to_human_size, but…
369 end
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
370
371 DECIMAL_UNITS = {0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand, 6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion,
372 -1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro, -9 => :nano, -12 => :pico, -15 => :femto}.freeze
373
374 # Pretty prints (formats and approximates) a number in a way it is more readable by humans
375 # (eg.: 1200000000 becomes "1.2 Billion"). This is useful for numbers that
376 # can get very large (and too hard to read).
377 #
378 # See <tt>number_to_human_size</tt> if you want to print a file size.
379 #
380 # You can also define you own unit-quantifier names if you want to use other decimal units
87e9e3f9 » rtlechow
2011-03-04 Action Pack typos.
381 # (eg.: 1500 becomes "1.5 kilometers", 0.150 becomes "150 milliliters", etc). You may define
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
382 # a wide range of unit quantifiers, even fractional ones (centi, deci, mili, etc).
383 #
384 # ==== Options
5a745575 » basti
2010-06-11 Updated all number helpers with :locale option.
385 # * <tt>:locale</tt> - Sets the locale to be used for formatting (defaults to current locale).
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
386 # * <tt>:precision</tt> - Sets the precision of the number (defaults to 3).
387 # * <tt>:significant</tt> - If +true+, precision will be the # of significant_digits. If +false+, the # of fractional digits (defaults to +true+)
388 # * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to ".").
389 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
390 # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator (defaults to +true+)
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
391 # * <tt>:units</tt> - A Hash of unit quantifier names. Or a string containing an i18n scope where to find this hash. It might have the following keys:
392 # * *integers*: <tt>:unit</tt>, <tt>:ten</tt>, <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>, <tt>:billion</tt>, <tt>:trillion</tt>, <tt>:quadrillion</tt>
393 # * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>, <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>, <tt>:pico</tt>, <tt>:femto</tt>
394 # * <tt>:format</tt> - Sets the format of the output string (defaults to "%n %u"). The field types are:
395 # %u The quantifier (ex.: 'thousand')
396 # %n The number
848276df » vijaydev
2012-04-26 fix number_to_human docs [ci skip]
397 # * <tt>:raise</tt> - If true, raises +InvalidNumberError+ when the argument is invalid.
398 #
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
399 #
400 # ==== Examples
401 # number_to_human(123) # => "123"
402 # number_to_human(1234) # => "1.23 Thousand"
403 # number_to_human(12345) # => "12.3 Thousand"
404 # number_to_human(1234567) # => "1.23 Million"
405 # number_to_human(1234567890) # => "1.23 Billion"
406 # number_to_human(1234567890123) # => "1.23 Trillion"
407 # number_to_human(1234567890123456) # => "1.23 Quadrillion"
408 # number_to_human(1234567890123456789) # => "1230 Quadrillion"
409 # number_to_human(489939, :precision => 2) # => "490 Thousand"
410 # number_to_human(489939, :precision => 4) # => "489.9 Thousand"
411 # number_to_human(1234567, :precision => 4,
412 # :significant => false) # => "1.2346 Million"
413 # number_to_human(1234567, :precision => 1,
414 # :separator => ',',
415 # :significant => false) # => "1,2 Million"
416 #
417 # Unsignificant zeros after the decimal separator are stripped out by default (set
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
418 # <tt>:strip_insignificant_zeros</tt> to +false+ to change that):
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
419 # number_to_human(12345012345, :significant_digits => 6) # => "12.345 Billion"
e8afe4e1 » mrduncan
2011-04-15 Making spacing consistent
420 # number_to_human(500000000, :precision => 5) # => "500 Million"
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
421 #
422 # ==== Custom Unit Quantifiers
423 #
424 # You can also use your own custom unit quantifiers:
425 # number_to_human(500000, :units => {:unit => "ml", :thousand => "lt"}) # => "500 lt"
426 #
427 # If in your I18n locale you have:
428 # distance:
429 # centi:
430 # one: "centimeter"
431 # other: "centimeters"
432 # unit:
433 # one: "meter"
434 # other: "meters"
435 # thousand:
436 # one: "kilometer"
437 # other: "kilometers"
87e9e3f9 » rtlechow
2011-03-04 Action Pack typos.
438 # billion: "gazillion-distance"
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
439 #
440 # Then you could do:
441 #
442 # number_to_human(543934, :units => :distance) # => "544 kilometers"
443 # number_to_human(54393498, :units => :distance) # => "54400 kilometers"
87e9e3f9 » rtlechow
2011-03-04 Action Pack typos.
444 # number_to_human(54393498000, :units => :distance) # => "54.4 gazillion-distance"
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
445 # number_to_human(343, :units => :distance, :precision => 1) # => "300 meters"
446 # number_to_human(1, :units => :distance) # => "1 meter"
447 # number_to_human(0.34, :units => :distance) # => "34 centimeters"
448 #
449 def number_to_human(number, options = {})
74fabdf6 » carlosantoniodasilva
2012-01-25 Do not mutate given options hash in number helpers
450 options = options.symbolize_keys
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
451
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
452 number = (parse_float(number, options[:raise]) or return number)
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
453
fb22f93b » carlosantoniodasilva
2012-01-21 Improve getting translations for number helpers
454 defaults = format_translations('human', options[:locale])
455 options = defaults.merge!(options)
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
456
c53f77f3 » josevalim
2010-03-23 Rename unsignificant to insignificant.
457 #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
458 options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros)
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
459
dda81cb1 » tenderlove
2010-10-18 lets not invert the hash on every iteration through this loop
460 inverted_du = DECIMAL_UNITS.invert
461
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
462 units = options.delete :units
463 unit_exponents = case units
464 when Hash
465 units
466 when String, Symbol
467 I18n.translate(:"#{units}", :locale => options[:locale], :raise => true)
468 when nil
469 I18n.translate(:"number.human.decimal_units.units", :locale => options[:locale], :raise => true)
470 else
471 raise ArgumentError, ":units must be a Hash or String translation scope."
dda81cb1 » tenderlove
2010-10-18 lets not invert the hash on every iteration through this loop
472 end.keys.map{|e_name| inverted_du[e_name] }.sort_by{|e| -e}
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
473
e20012b6 » spastorino
2010-09-05 Make number_to_human and number_with_precision work with negatives
474 number_exponent = number != 0 ? Math.log10(number.abs).floor : 0
0eae6252 » joshk
2011-03-16 fixes an issue with number_to_human when converting values which are …
475 display_exponent = unit_exponents.find{ |e| number_exponent >= e } || 0
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
476 number /= 10 ** display_exponent
477
478 unit = case units
479 when Hash
480 units[DECIMAL_UNITS[display_exponent]]
481 when String, Symbol
482 I18n.translate(:"#{units}.#{DECIMAL_UNITS[display_exponent]}", :locale => options[:locale], :count => number.to_i)
483 else
484 I18n.translate(:"number.human.decimal_units.units.#{DECIMAL_UNITS[display_exponent]}", :locale => options[:locale], :count => number.to_i)
485 end
486
487 decimal_format = options[:format] || I18n.translate(:'number.human.decimal_units.format', :locale => options[:locale], :default => "%n %u")
488 formatted_number = number_with_precision(number, options)
a4090bca » bernardo
2010-03-22 NumberHelper methods should now return html_safe strings (when the in…
489 decimal_format.gsub(/%n/, formatted_number).gsub(/%u/, unit).strip.html_safe
75904c56 » bernardo
2010-03-20 Adds number_to_human and several improvements in NumberHelper. [#4239
490 end
491
f1704534 » rafaelfranca
2012-01-20 Remove code duplication
492 private
493
fb22f93b » carlosantoniodasilva
2012-01-21 Improve getting translations for number helpers
494 def format_translations(namespace, locale)
495 defaults_translations(locale).merge(translations_for(namespace, locale))
496 end
497
977f6b49 » rafaelfranca
2012-01-20 Refactor translations retrieval
498 def defaults_translations(locale)
499 I18n.translate(:'number.format', :locale => locale, :default => {})
500 end
501
502 def translations_for(namespace, locale)
503 I18n.translate(:"number.#{namespace}.format", :locale => locale, :default => {})
504 end
505
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
506 def parse_float(number, raise_error)
f1704534 » rafaelfranca
2012-01-20 Remove code duplication
507 Float(number)
508 rescue ArgumentError, TypeError
1e78fed4 » carlosantoniodasilva
2012-01-21 Refactor float number parsing
509 raise InvalidNumberError, number if raise_error
f1704534 » rafaelfranca
2012-01-20 Remove code duplication
510 end
aa09c770 » dhh
2005-04-02 Added NumberHelper for common string representations like phone numbe…
511 end
512 end
5cd815ad » dhh
2005-04-10 Fixed spelling of delimiter #1058
513 end
Something went wrong with that request. Please try again.