activesupport 7.1.2 → 7.1.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +57 -0
- data/lib/active_support/cache/mem_cache_store.rb +6 -6
- data/lib/active_support/cache/memory_store.rb +4 -4
- data/lib/active_support/cache/redis_cache_store.rb +16 -12
- data/lib/active_support/cache/strategy/local_cache.rb +9 -6
- data/lib/active_support/cache.rb +15 -5
- data/lib/active_support/core_ext/object/with_options.rb +1 -1
- data/lib/active_support/core_ext/string/indent.rb +1 -1
- data/lib/active_support/deprecation/behaviors.rb +18 -16
- data/lib/active_support/deprecation/reporting.rb +7 -4
- data/lib/active_support/gem_version.rb +2 -2
- data/lib/active_support/html_safe_translation.rb +12 -2
- data/lib/active_support/json/encoding.rb +1 -1
- data/lib/active_support/number_helper.rb +379 -318
- data/lib/active_support/syntax_error_proxy.rb +22 -1
- data/lib/active_support/testing/assertions.rb +1 -1
- data/lib/active_support/testing/time_helpers.rb +4 -0
- metadata +6 -6
@@ -18,377 +18,438 @@ module ActiveSupport
|
|
18
18
|
|
19
19
|
extend self
|
20
20
|
|
21
|
-
# Formats
|
22
|
-
# 123-9876). You can customize the format in the +options+ hash.
|
21
|
+
# Formats +number+ into a phone number.
|
23
22
|
#
|
24
|
-
#
|
23
|
+
# number_to_phone(5551234) # => "555-1234"
|
24
|
+
# number_to_phone("5551234") # => "555-1234"
|
25
|
+
# number_to_phone(1235551234) # => "123-555-1234"
|
26
|
+
# number_to_phone("12x34") # => "12x34"
|
25
27
|
#
|
26
|
-
#
|
27
|
-
# * <tt>:delimiter</tt> - Specifies the delimiter to use
|
28
|
-
# (defaults to "-").
|
29
|
-
# * <tt>:extension</tt> - Specifies an extension to add to the
|
30
|
-
# end of the generated number.
|
31
|
-
# * <tt>:country_code</tt> - Sets the country code for the phone
|
32
|
-
# number.
|
33
|
-
# * <tt>:pattern</tt> - Specifies how the number is divided into three
|
34
|
-
# groups with the custom regexp to override the default format.
|
35
|
-
# ==== Examples
|
36
|
-
#
|
37
|
-
# number_to_phone(5551234) # => "555-1234"
|
38
|
-
# number_to_phone('5551234') # => "555-1234"
|
39
|
-
# number_to_phone(1235551234) # => "123-555-1234"
|
40
|
-
# number_to_phone(1235551234, area_code: true) # => "(123) 555-1234"
|
41
|
-
# number_to_phone(1235551234, delimiter: ' ') # => "123 555 1234"
|
42
|
-
# number_to_phone(1235551234, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
|
43
|
-
# number_to_phone(1235551234, country_code: 1) # => "+1-123-555-1234"
|
44
|
-
# number_to_phone('123a456') # => "123a456"
|
45
|
-
#
|
46
|
-
# number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.')
|
28
|
+
# number_to_phone(1235551234, delimiter: ".", country_code: 1, extension: 1343)
|
47
29
|
# # => "+1.123.555.1234 x 1343"
|
48
30
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
31
|
+
# ==== Options
|
32
|
+
#
|
33
|
+
# [+:area_code+]
|
34
|
+
# Whether to use parentheses for the area code. Defaults to false.
|
35
|
+
#
|
36
|
+
# number_to_phone(1235551234, area_code: true)
|
37
|
+
# # => "(123) 555-1234"
|
38
|
+
#
|
39
|
+
# [+:delimiter+]
|
40
|
+
# The digit group delimiter to use. Defaults to <tt>"-"</tt>.
|
41
|
+
#
|
42
|
+
# number_to_phone(1235551234, delimiter: " ")
|
43
|
+
# # => "123 555 1234"
|
44
|
+
#
|
45
|
+
# [+:country_code+]
|
46
|
+
# A country code to prepend.
|
47
|
+
#
|
48
|
+
# number_to_phone(1235551234, country_code: 1)
|
49
|
+
# # => "+1-123-555-1234"
|
50
|
+
#
|
51
|
+
# [+:extension+]
|
52
|
+
# An extension to append.
|
53
|
+
#
|
54
|
+
# number_to_phone(1235551234, extension: 555)
|
55
|
+
# # => "123-555-1234 x 555"
|
56
|
+
#
|
57
|
+
# [+:pattern+]
|
58
|
+
# A regexp that specifies how the digits should be grouped. The first
|
59
|
+
# three captures from the regexp are treated as digit groups.
|
60
|
+
#
|
61
|
+
# number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
|
62
|
+
# # => "133-1234-5678"
|
63
|
+
# number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
|
64
|
+
# # => "(755) 6123-4567"
|
65
|
+
#
|
53
66
|
def number_to_phone(number, options = {})
|
54
67
|
NumberToPhoneConverter.convert(number, options)
|
55
68
|
end
|
56
69
|
|
57
|
-
# Formats a +number+ into a currency string
|
58
|
-
#
|
70
|
+
# Formats a +number+ into a currency string.
|
71
|
+
#
|
72
|
+
# number_to_currency(1234567890.50) # => "$1,234,567,890.50"
|
73
|
+
# number_to_currency(1234567890.506) # => "$1,234,567,890.51"
|
74
|
+
# number_to_currency("12x34") # => "$12x34"
|
75
|
+
#
|
76
|
+
# number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "")
|
77
|
+
# # => "£1234567890,50"
|
59
78
|
#
|
60
79
|
# The currency unit and number formatting of the current locale will be used
|
61
|
-
# unless otherwise specified
|
62
|
-
#
|
80
|
+
# unless otherwise specified via options. No currency conversion is
|
81
|
+
# performed. If the user is given a way to change their locale, they will
|
63
82
|
# also be able to change the relative value of the currency displayed with
|
64
83
|
# this helper. If your application will ever support multiple locales, you
|
65
|
-
# may want to specify a constant
|
66
|
-
#
|
84
|
+
# may want to specify a constant +:locale+ option or consider using a
|
85
|
+
# library capable of currency conversion.
|
67
86
|
#
|
68
87
|
# ==== Options
|
69
88
|
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
89
|
+
# [+:locale+]
|
90
|
+
# The locale to use for formatting. Defaults to the current locale.
|
91
|
+
#
|
92
|
+
# number_to_currency(1234567890.506, locale: :fr)
|
93
|
+
# # => "1 234 567 890,51 €"
|
94
|
+
#
|
95
|
+
# [+:precision+]
|
96
|
+
# The level of precision. Defaults to 2.
|
97
|
+
#
|
98
|
+
# number_to_currency(1234567890.123, precision: 3) # => "$1,234,567,890.123"
|
99
|
+
# number_to_currency(0.456789, precision: 0) # => "$0"
|
100
|
+
#
|
101
|
+
# [+:round_mode+]
|
102
|
+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
|
103
|
+
# +:default+.
|
104
|
+
#
|
105
|
+
# number_to_currency(1234567890.01, precision: 0, round_mode: :up)
|
106
|
+
# # => "$1,234,567,891"
|
107
|
+
#
|
108
|
+
# [+:unit+]
|
109
|
+
# The denomination of the currency. Defaults to <tt>"$"</tt>.
|
110
|
+
#
|
111
|
+
# [+:separator+]
|
112
|
+
# The decimal separator. Defaults to <tt>"."</tt>.
|
113
|
+
#
|
114
|
+
# [+:delimiter+]
|
115
|
+
# The thousands delimiter. Defaults to <tt>","</tt>.
|
116
|
+
#
|
117
|
+
# [+:format+]
|
118
|
+
# The format for non-negative numbers. <tt>%u</tt> represents the currency,
|
119
|
+
# and <tt>%n</tt> represents the number. Defaults to <tt>"%u%n"</tt>.
|
120
|
+
#
|
121
|
+
# number_to_currency(1234567890.50, format: "%n %u")
|
122
|
+
# # => "1,234,567,890.50 $"
|
123
|
+
#
|
124
|
+
# [+:negative_format+]
|
125
|
+
# The format for negative numbers. <tt>%u</tt> and <tt>%n</tt> behave the
|
126
|
+
# same as in +:format+, but <tt>%n</tt> represents the absolute value of
|
127
|
+
# the number. Defaults to the value of +:format+ prepended with <tt>-</tt>.
|
128
|
+
#
|
129
|
+
# number_to_currency(-1234567890.50, negative_format: "(%u%n)")
|
130
|
+
# # => "($1,234,567,890.50)"
|
131
|
+
#
|
132
|
+
# [+:strip_insignificant_zeros+]
|
133
|
+
# Whether to remove insignificant zeros after the decimal separator.
|
134
|
+
# Defaults to false.
|
135
|
+
#
|
136
|
+
# number_to_currency(1234567890.50, strip_insignificant_zeros: true)
|
137
|
+
# # => "$1,234,567,890.5"
|
138
|
+
#
|
114
139
|
def number_to_currency(number, options = {})
|
115
140
|
NumberToCurrencyConverter.convert(number, options)
|
116
141
|
end
|
117
142
|
|
118
|
-
# Formats
|
119
|
-
#
|
143
|
+
# Formats +number+ as a percentage string.
|
144
|
+
#
|
145
|
+
# number_to_percentage(100) # => "100.000%"
|
146
|
+
# number_to_percentage("99") # => "99.000%"
|
147
|
+
# number_to_percentage("99x") # => "99x%"
|
148
|
+
#
|
149
|
+
# number_to_percentage(12345.6789, delimiter: ".", separator: ",", precision: 2)
|
150
|
+
# # => "12.345,68%"
|
120
151
|
#
|
121
152
|
# ==== Options
|
122
153
|
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
# of
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
# +
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
+
# [+:locale+]
|
155
|
+
# The locale to use for formatting. Defaults to the current locale.
|
156
|
+
#
|
157
|
+
# number_to_percentage(1000, locale: :fr)
|
158
|
+
# # => "1000,000%"
|
159
|
+
#
|
160
|
+
# [+:precision+]
|
161
|
+
# The level of precision, or +nil+ to preserve +number+'s precision.
|
162
|
+
# Defaults to 2.
|
163
|
+
#
|
164
|
+
# number_to_percentage(12.3456789, precision: 4) # => "12.3457%"
|
165
|
+
# number_to_percentage(99.999, precision: 0) # => "100%"
|
166
|
+
# number_to_percentage(99.999, precision: nil) # => "99.999%"
|
167
|
+
#
|
168
|
+
# [+:round_mode+]
|
169
|
+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
|
170
|
+
# +:default+.
|
171
|
+
#
|
172
|
+
# number_to_percentage(12.3456789, precision: 4, round_mode: :down)
|
173
|
+
# # => "12.3456%"
|
174
|
+
#
|
175
|
+
# [+:significant+]
|
176
|
+
# Whether +:precision+ should be applied to significant digits instead of
|
177
|
+
# fractional digits. Defaults to false.
|
178
|
+
#
|
179
|
+
# number_to_percentage(12345.6789) # => "12345.679%"
|
180
|
+
# number_to_percentage(12345.6789, significant: true) # => "12300%"
|
181
|
+
# number_to_percentage(12345.6789, precision: 2) # => "12345.68%"
|
182
|
+
# number_to_percentage(12345.6789, precision: 2, significant: true) # => "12000%"
|
183
|
+
#
|
184
|
+
# [+:separator+]
|
185
|
+
# The decimal separator. Defaults to <tt>"."</tt>.
|
186
|
+
#
|
187
|
+
# [+:delimiter+]
|
188
|
+
# The thousands delimiter. Defaults to <tt>","</tt>.
|
189
|
+
#
|
190
|
+
# [+:strip_insignificant_zeros+]
|
191
|
+
# Whether to remove insignificant zeros after the decimal separator.
|
192
|
+
# Defaults to false.
|
193
|
+
#
|
194
|
+
# [+:format+]
|
195
|
+
# The format of the output. <tt>%n</tt> represents the number. Defaults to
|
196
|
+
# <tt>"%n%"</tt>.
|
197
|
+
#
|
198
|
+
# number_to_percentage(100, format: "%n %")
|
199
|
+
# # => "100.000 %"
|
200
|
+
#
|
154
201
|
def number_to_percentage(number, options = {})
|
155
202
|
NumberToPercentageConverter.convert(number, options)
|
156
203
|
end
|
157
204
|
|
158
|
-
# Formats
|
159
|
-
#
|
160
|
-
#
|
205
|
+
# Formats +number+ by grouping thousands with a delimiter.
|
206
|
+
#
|
207
|
+
# number_to_delimited(12345678) # => "12,345,678"
|
208
|
+
# number_to_delimited("123456") # => "123,456"
|
209
|
+
# number_to_delimited(12345678.9876) # => "12,345,678.9876"
|
210
|
+
# number_to_delimited("12x34") # => "12x34"
|
211
|
+
#
|
212
|
+
# number_to_delimited(12345678.9876, delimiter: ".", separator: ",")
|
213
|
+
# # => "12.345.678,9876"
|
161
214
|
#
|
162
215
|
# ==== Options
|
163
216
|
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
217
|
+
# [+:locale+]
|
218
|
+
# The locale to use for formatting. Defaults to the current locale.
|
219
|
+
#
|
220
|
+
# number_to_delimited(12345678.05, locale: :fr)
|
221
|
+
# # => "12 345 678,05"
|
222
|
+
#
|
223
|
+
# [+:delimiter+]
|
224
|
+
# The thousands delimiter. Defaults to <tt>","</tt>.
|
225
|
+
#
|
226
|
+
# number_to_delimited(12345678, delimiter: ".")
|
227
|
+
# # => "12.345.678"
|
228
|
+
#
|
229
|
+
# [+:separator+]
|
230
|
+
# The decimal separator. Defaults to <tt>"."</tt>.
|
231
|
+
#
|
232
|
+
# number_to_delimited(12345678.05, separator: " ")
|
233
|
+
# # => "12,345,678 05"
|
234
|
+
#
|
235
|
+
# [+:delimiter_pattern+]
|
236
|
+
# A regexp to determine the placement of delimiters. Helpful when using
|
237
|
+
# currency formats like INR.
|
238
|
+
#
|
239
|
+
# number_to_delimited("123456.78", delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
|
240
|
+
# # => "1,23,456.78"
|
241
|
+
#
|
189
242
|
def number_to_delimited(number, options = {})
|
190
243
|
NumberToDelimitedConverter.convert(number, options)
|
191
244
|
end
|
192
245
|
|
193
|
-
# Formats
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
246
|
+
# Formats +number+ to a specific level of precision.
|
247
|
+
#
|
248
|
+
# number_to_rounded(12345.6789) # => "12345.679"
|
249
|
+
# number_to_rounded(12345.6789, precision: 2) # => "12345.68"
|
250
|
+
# number_to_rounded(12345.6789, precision: 0) # => "12345"
|
251
|
+
# number_to_rounded(12345, precision: 5) # => "12345.00000"
|
197
252
|
#
|
198
253
|
# ==== Options
|
199
254
|
#
|
200
|
-
#
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
205
|
-
#
|
206
|
-
#
|
207
|
-
# of
|
208
|
-
#
|
209
|
-
#
|
210
|
-
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
#
|
228
|
-
#
|
229
|
-
#
|
230
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
255
|
+
# [+:locale+]
|
256
|
+
# The locale to use for formatting. Defaults to the current locale.
|
257
|
+
#
|
258
|
+
# number_to_rounded(111.234, locale: :fr)
|
259
|
+
# # => "111,234"
|
260
|
+
#
|
261
|
+
# [+:precision+]
|
262
|
+
# The level of precision, or +nil+ to preserve +number+'s precision.
|
263
|
+
# Defaults to 3.
|
264
|
+
#
|
265
|
+
# number_to_rounded(12345.6789, precision: nil)
|
266
|
+
# # => "12345.6789"
|
267
|
+
#
|
268
|
+
# [+:round_mode+]
|
269
|
+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
|
270
|
+
# +:default+.
|
271
|
+
#
|
272
|
+
# number_to_rounded(12.34, precision: 0, round_mode: :up)
|
273
|
+
# # => "13"
|
274
|
+
#
|
275
|
+
# [+:significant+]
|
276
|
+
# Whether +:precision+ should be applied to significant digits instead of
|
277
|
+
# fractional digits. Defaults to false.
|
278
|
+
#
|
279
|
+
# number_to_rounded(12345.6789) # => "12345.679"
|
280
|
+
# number_to_rounded(12345.6789, significant: true) # => "12300"
|
281
|
+
# number_to_rounded(12345.6789, precision: 2) # => "12345.68"
|
282
|
+
# number_to_rounded(12345.6789, precision: 2, significant: true) # => "12000"
|
283
|
+
#
|
284
|
+
# [+:separator+]
|
285
|
+
# The decimal separator. Defaults to <tt>"."</tt>.
|
286
|
+
#
|
287
|
+
# [+:delimiter+]
|
288
|
+
# The thousands delimiter. Defaults to <tt>","</tt>.
|
289
|
+
#
|
290
|
+
# [+:strip_insignificant_zeros+]
|
291
|
+
# Whether to remove insignificant zeros after the decimal separator.
|
292
|
+
# Defaults to false.
|
293
|
+
#
|
294
|
+
# number_to_rounded(12.34, strip_insignificant_zeros: false) # => "12.340"
|
295
|
+
# number_to_rounded(12.34, strip_insignificant_zeros: true) # => "12.34"
|
296
|
+
# number_to_rounded(12.3456, strip_insignificant_zeros: true) # => "12.346"
|
297
|
+
#
|
236
298
|
def number_to_rounded(number, options = {})
|
237
299
|
NumberToRoundedConverter.convert(number, options)
|
238
300
|
end
|
239
301
|
|
240
|
-
# Formats
|
241
|
-
#
|
242
|
-
#
|
243
|
-
#
|
302
|
+
# Formats +number+ as bytes into a more human-friendly representation.
|
303
|
+
# Useful for reporting file sizes to users.
|
304
|
+
#
|
305
|
+
# number_to_human_size(123) # => "123 Bytes"
|
306
|
+
# number_to_human_size(1234) # => "1.21 KB"
|
307
|
+
# number_to_human_size(12345) # => "12.1 KB"
|
308
|
+
# number_to_human_size(1234567) # => "1.18 MB"
|
309
|
+
# number_to_human_size(1234567890) # => "1.15 GB"
|
310
|
+
# number_to_human_size(1234567890123) # => "1.12 TB"
|
311
|
+
# number_to_human_size(1234567890123456) # => "1.1 PB"
|
312
|
+
# number_to_human_size(1234567890123456789) # => "1.07 EB"
|
244
313
|
#
|
245
|
-
# See
|
246
|
-
# generic number.
|
314
|
+
# See #number_to_human if you want to pretty-print a generic number.
|
247
315
|
#
|
248
316
|
# ==== Options
|
249
317
|
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
#
|
257
|
-
#
|
258
|
-
#
|
259
|
-
#
|
260
|
-
#
|
261
|
-
#
|
262
|
-
#
|
263
|
-
#
|
264
|
-
#
|
265
|
-
#
|
266
|
-
#
|
267
|
-
#
|
268
|
-
#
|
269
|
-
#
|
270
|
-
#
|
271
|
-
#
|
272
|
-
#
|
273
|
-
#
|
274
|
-
#
|
275
|
-
#
|
276
|
-
#
|
277
|
-
#
|
278
|
-
#
|
279
|
-
#
|
280
|
-
#
|
281
|
-
#
|
282
|
-
#
|
318
|
+
# [+:locale+]
|
319
|
+
# The locale to use for formatting. Defaults to the current locale.
|
320
|
+
#
|
321
|
+
# [+:precision+]
|
322
|
+
# The level of precision. Defaults to 3.
|
323
|
+
#
|
324
|
+
# number_to_human_size(123456, precision: 2) # => "120 KB"
|
325
|
+
# number_to_human_size(1234567, precision: 2) # => "1.2 MB"
|
326
|
+
#
|
327
|
+
# [+:round_mode+]
|
328
|
+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
|
329
|
+
# +:default+.
|
330
|
+
#
|
331
|
+
# number_to_human_size(123456, precision: 2, round_mode: :up)
|
332
|
+
# # => "130 KB"
|
333
|
+
#
|
334
|
+
# [+:significant+]
|
335
|
+
# Whether +:precision+ should be applied to significant digits instead of
|
336
|
+
# fractional digits. Defaults to true.
|
337
|
+
#
|
338
|
+
# [+:separator+]
|
339
|
+
# The decimal separator. Defaults to <tt>"."</tt>.
|
340
|
+
#
|
341
|
+
# number_to_human_size(1234567, separator: ",")
|
342
|
+
# # => "1,18 MB"
|
343
|
+
#
|
344
|
+
# [+:delimiter+]
|
345
|
+
# The thousands delimiter. Defaults to <tt>","</tt>.
|
346
|
+
#
|
347
|
+
# [+:strip_insignificant_zeros+]
|
348
|
+
# Whether to remove insignificant zeros after the decimal separator.
|
349
|
+
# Defaults to true.
|
350
|
+
#
|
283
351
|
def number_to_human_size(number, options = {})
|
284
352
|
NumberToHumanSizeConverter.convert(number, options)
|
285
353
|
end
|
286
354
|
|
287
|
-
#
|
288
|
-
#
|
289
|
-
# Billion"). This is useful for numbers that can get very large
|
290
|
-
# (and too hard to read).
|
355
|
+
# Formats +number+ into a more human-friendly representation. Useful for
|
356
|
+
# numbers that can become very large and too hard to read.
|
291
357
|
#
|
292
|
-
#
|
293
|
-
#
|
358
|
+
# number_to_human(123) # => "123"
|
359
|
+
# number_to_human(1234) # => "1.23 Thousand"
|
360
|
+
# number_to_human(12345) # => "12.3 Thousand"
|
361
|
+
# number_to_human(1234567) # => "1.23 Million"
|
362
|
+
# number_to_human(1234567890) # => "1.23 Billion"
|
363
|
+
# number_to_human(1234567890123) # => "1.23 Trillion"
|
364
|
+
# number_to_human(1234567890123456) # => "1.23 Quadrillion"
|
365
|
+
# number_to_human(1234567890123456789) # => "1230 Quadrillion"
|
294
366
|
#
|
295
|
-
#
|
296
|
-
# to use other decimal units (e.g.: 1500 becomes "1.5
|
297
|
-
# kilometers", 0.150 becomes "150 milliliters", etc). You may
|
298
|
-
# define a wide range of unit quantifiers, even fractional ones
|
299
|
-
# (centi, deci, mili, etc).
|
367
|
+
# See #number_to_human_size if you want to pretty-print a file size.
|
300
368
|
#
|
301
369
|
# ==== Options
|
302
370
|
#
|
303
|
-
#
|
304
|
-
#
|
305
|
-
#
|
306
|
-
#
|
307
|
-
#
|
308
|
-
#
|
309
|
-
#
|
310
|
-
#
|
311
|
-
#
|
312
|
-
#
|
313
|
-
#
|
314
|
-
#
|
315
|
-
#
|
316
|
-
#
|
317
|
-
#
|
318
|
-
#
|
319
|
-
#
|
320
|
-
#
|
321
|
-
#
|
322
|
-
#
|
323
|
-
#
|
324
|
-
#
|
325
|
-
#
|
326
|
-
#
|
327
|
-
#
|
328
|
-
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
#
|
332
|
-
#
|
333
|
-
#
|
334
|
-
#
|
335
|
-
#
|
336
|
-
#
|
337
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#
|
343
|
-
#
|
344
|
-
#
|
345
|
-
#
|
346
|
-
#
|
347
|
-
#
|
348
|
-
#
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
#
|
353
|
-
#
|
354
|
-
#
|
355
|
-
#
|
356
|
-
#
|
357
|
-
#
|
358
|
-
#
|
359
|
-
#
|
360
|
-
#
|
361
|
-
#
|
362
|
-
#
|
363
|
-
#
|
364
|
-
#
|
365
|
-
#
|
366
|
-
#
|
367
|
-
#
|
368
|
-
#
|
369
|
-
#
|
370
|
-
#
|
371
|
-
#
|
372
|
-
#
|
373
|
-
#
|
374
|
-
#
|
375
|
-
#
|
376
|
-
#
|
377
|
-
#
|
378
|
-
#
|
379
|
-
#
|
380
|
-
#
|
381
|
-
#
|
382
|
-
#
|
383
|
-
#
|
384
|
-
#
|
385
|
-
#
|
386
|
-
# number_to_human(543934, units: :distance) # => "544 kilometers"
|
387
|
-
# number_to_human(54393498, units: :distance) # => "54400 kilometers"
|
388
|
-
# number_to_human(54393498000, units: :distance) # => "54.4 gazillion-distance"
|
389
|
-
# number_to_human(343, units: :distance, precision: 1) # => "300 meters"
|
390
|
-
# number_to_human(1, units: :distance) # => "1 meter"
|
391
|
-
# number_to_human(0.34, units: :distance) # => "34 centimeters"
|
371
|
+
# [+:locale+]
|
372
|
+
# The locale to use for formatting. Defaults to the current locale.
|
373
|
+
#
|
374
|
+
# [+:precision+]
|
375
|
+
# The level of precision. Defaults to 3.
|
376
|
+
#
|
377
|
+
# number_to_human(123456, precision: 2) # => "120 Thousand"
|
378
|
+
# number_to_human(123456, precision: 4) # => "123.5 Thousand"
|
379
|
+
#
|
380
|
+
# [+:round_mode+]
|
381
|
+
# Specifies how rounding is performed. See +BigDecimal.mode+. Defaults to
|
382
|
+
# +:default+.
|
383
|
+
#
|
384
|
+
# number_to_human(123456, precision: 2, round_mode: :up)
|
385
|
+
# # => "130 Thousand"
|
386
|
+
#
|
387
|
+
# [+:significant+]
|
388
|
+
# Whether +:precision+ should be applied to significant digits instead of
|
389
|
+
# fractional digits. Defaults to true.
|
390
|
+
#
|
391
|
+
# [+:separator+]
|
392
|
+
# The decimal separator. Defaults to <tt>"."</tt>.
|
393
|
+
#
|
394
|
+
# number_to_human(123456, precision: 4, separator: ",")
|
395
|
+
# # => "123,5 Thousand"
|
396
|
+
#
|
397
|
+
# [+:delimiter+]
|
398
|
+
# The thousands delimiter. Defaults to <tt>","</tt>.
|
399
|
+
#
|
400
|
+
# [+:strip_insignificant_zeros+]
|
401
|
+
# Whether to remove insignificant zeros after the decimal separator.
|
402
|
+
# Defaults to true.
|
403
|
+
#
|
404
|
+
# number_to_human(1000000) # => "1 Million"
|
405
|
+
# number_to_human(1000000, strip_insignificant_zeros: false) # => "1.00 Million"
|
406
|
+
# number_to_human(10.01) # => "10"
|
407
|
+
# number_to_human(10.01, strip_insignificant_zeros: false) # => "10.0"
|
408
|
+
#
|
409
|
+
# [+:format+]
|
410
|
+
# The format of the output. <tt>%n</tt> represents the number, and
|
411
|
+
# <tt>%u</tt> represents the quantifier (e.g., "Thousand"). Defaults to
|
412
|
+
# <tt>"%n %u"</tt>.
|
413
|
+
#
|
414
|
+
# [+:units+]
|
415
|
+
# A Hash of custom unit quantifier names.
|
416
|
+
#
|
417
|
+
# number_to_human(1, units: { unit: "m", thousand: "km" }) # => "1 m"
|
418
|
+
# number_to_human(100, units: { unit: "m", thousand: "km" }) # => "100 m"
|
419
|
+
# number_to_human(1000, units: { unit: "m", thousand: "km" }) # => "1 km"
|
420
|
+
# number_to_human(100000, units: { unit: "m", thousand: "km" }) # => "100 km"
|
421
|
+
# number_to_human(10000000, units: { unit: "m", thousand: "km" }) # => "10000 km"
|
422
|
+
#
|
423
|
+
# The following keys are supported for integer units: +:unit+, +:ten+,
|
424
|
+
# +:hundred+, +:thousand+, +:million+, +:billion+, +:trillion+,
|
425
|
+
# +:quadrillion+. Additionally, the following keys are supported for
|
426
|
+
# fractional units: +:deci+, +:centi+, +:mili+, +:micro+, +:nano+,
|
427
|
+
# +:pico+, +:femto+.
|
428
|
+
#
|
429
|
+
# The Hash can also be defined as a scope in an I18n locale. For example:
|
430
|
+
#
|
431
|
+
# en:
|
432
|
+
# distance:
|
433
|
+
# centi:
|
434
|
+
# one: "centimeter"
|
435
|
+
# other: "centimeters"
|
436
|
+
# unit:
|
437
|
+
# one: "meter"
|
438
|
+
# other: "meters"
|
439
|
+
# thousand:
|
440
|
+
# one: "kilometer"
|
441
|
+
# other: "kilometers"
|
442
|
+
#
|
443
|
+
# Then it can be specified by name:
|
444
|
+
#
|
445
|
+
# number_to_human(1, units: :distance) # => "1 meter"
|
446
|
+
# number_to_human(100, units: :distance) # => "100 meters"
|
447
|
+
# number_to_human(1000, units: :distance) # => "1 kilometer"
|
448
|
+
# number_to_human(100000, units: :distance) # => "100 kilometers"
|
449
|
+
# number_to_human(10000000, units: :distance) # => "10000 kilometers"
|
450
|
+
# number_to_human(0.1, units: :distance) # => "10 centimeters"
|
451
|
+
# number_to_human(0.01, units: :distance) # => "1 centimeter"
|
452
|
+
#
|
392
453
|
def number_to_human(number, options = {})
|
393
454
|
NumberToHumanConverter.convert(number, options)
|
394
455
|
end
|