activesupport 7.0.10 → 7.1.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +703 -361
- data/MIT-LICENSE +1 -1
- data/README.rdoc +4 -4
- data/lib/active_support/actionable_error.rb +3 -1
- data/lib/active_support/array_inquirer.rb +2 -0
- data/lib/active_support/backtrace_cleaner.rb +25 -5
- data/lib/active_support/benchmarkable.rb +1 -0
- data/lib/active_support/builder.rb +1 -1
- data/lib/active_support/cache/coder.rb +153 -0
- data/lib/active_support/cache/entry.rb +128 -0
- data/lib/active_support/cache/file_store.rb +37 -10
- data/lib/active_support/cache/mem_cache_store.rb +84 -68
- data/lib/active_support/cache/memory_store.rb +76 -26
- data/lib/active_support/cache/null_store.rb +6 -0
- data/lib/active_support/cache/redis_cache_store.rb +126 -131
- data/lib/active_support/cache/serializer_with_fallback.rb +175 -0
- data/lib/active_support/cache/strategy/local_cache.rb +20 -8
- data/lib/active_support/cache.rb +304 -246
- data/lib/active_support/callbacks.rb +38 -18
- data/lib/active_support/concern.rb +4 -2
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +42 -3
- data/lib/active_support/concurrency/null_lock.rb +13 -0
- data/lib/active_support/configurable.rb +10 -0
- data/lib/active_support/core_ext/array/conversions.rb +2 -1
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/class/attribute.rb +1 -0
- data/lib/active_support/core_ext/class/subclasses.rb +13 -10
- data/lib/active_support/core_ext/date/conversions.rb +1 -0
- data/lib/active_support/core_ext/date.rb +0 -1
- data/lib/active_support/core_ext/date_and_time/calculations.rb +10 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +6 -2
- data/lib/active_support/core_ext/date_time.rb +0 -1
- data/lib/active_support/core_ext/digest/uuid.rb +1 -10
- data/lib/active_support/core_ext/enumerable.rb +3 -75
- data/lib/active_support/core_ext/erb/util.rb +196 -0
- data/lib/active_support/core_ext/hash/conversions.rb +1 -1
- data/lib/active_support/core_ext/module/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +34 -16
- data/lib/active_support/core_ext/module/concerning.rb +6 -6
- data/lib/active_support/core_ext/module/delegation.rb +40 -11
- data/lib/active_support/core_ext/module/deprecation.rb +15 -12
- data/lib/active_support/core_ext/module/introspection.rb +0 -1
- data/lib/active_support/core_ext/numeric/bytes.rb +9 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +2 -0
- data/lib/active_support/core_ext/numeric.rb +0 -1
- data/lib/active_support/core_ext/object/deep_dup.rb +16 -0
- data/lib/active_support/core_ext/object/duplicable.rb +15 -24
- data/lib/active_support/core_ext/object/inclusion.rb +13 -5
- data/lib/active_support/core_ext/object/instance_variables.rb +22 -12
- data/lib/active_support/core_ext/object/json.rb +14 -8
- data/lib/active_support/core_ext/object/with.rb +44 -0
- data/lib/active_support/core_ext/object/with_options.rb +4 -4
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/pathname/blank.rb +16 -0
- data/lib/active_support/core_ext/pathname/existence.rb +2 -0
- data/lib/active_support/core_ext/pathname.rb +1 -0
- data/lib/active_support/core_ext/range/conversions.rb +28 -7
- data/lib/active_support/core_ext/range/overlap.rb +12 -0
- data/lib/active_support/core_ext/range.rb +1 -2
- data/lib/active_support/core_ext/securerandom.rb +24 -12
- data/lib/active_support/core_ext/string/filters.rb +20 -14
- data/lib/active_support/core_ext/string/indent.rb +1 -1
- data/lib/active_support/core_ext/string/inflections.rb +16 -5
- data/lib/active_support/core_ext/string/output_safety.rb +38 -174
- data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
- data/lib/active_support/core_ext/time/calculations.rb +18 -2
- data/lib/active_support/core_ext/time/conversions.rb +2 -2
- data/lib/active_support/core_ext/time/zones.rb +4 -4
- data/lib/active_support/core_ext/time.rb +0 -1
- data/lib/active_support/current_attributes.rb +15 -6
- data/lib/active_support/dependencies/autoload.rb +17 -12
- data/lib/active_support/deprecation/behaviors.rb +55 -34
- data/lib/active_support/deprecation/constant_accessor.rb +5 -4
- data/lib/active_support/deprecation/deprecators.rb +104 -0
- data/lib/active_support/deprecation/disallowed.rb +3 -5
- data/lib/active_support/deprecation/instance_delegator.rb +31 -4
- data/lib/active_support/deprecation/method_wrappers.rb +6 -23
- data/lib/active_support/deprecation/proxy_wrappers.rb +37 -22
- data/lib/active_support/deprecation/reporting.rb +40 -29
- data/lib/active_support/deprecation.rb +32 -5
- data/lib/active_support/deprecator.rb +7 -0
- data/lib/active_support/descendants_tracker.rb +104 -132
- data/lib/active_support/duration/iso8601_serializer.rb +0 -2
- data/lib/active_support/duration.rb +2 -1
- data/lib/active_support/encrypted_configuration.rb +30 -9
- data/lib/active_support/encrypted_file.rb +8 -3
- data/lib/active_support/environment_inquirer.rb +22 -2
- data/lib/active_support/error_reporter/test_helper.rb +15 -0
- data/lib/active_support/error_reporter.rb +121 -35
- data/lib/active_support/execution_wrapper.rb +4 -4
- data/lib/active_support/file_update_checker.rb +4 -2
- data/lib/active_support/fork_tracker.rb +10 -2
- data/lib/active_support/gem_version.rb +4 -4
- data/lib/active_support/gzip.rb +2 -0
- data/lib/active_support/hash_with_indifferent_access.rb +35 -17
- data/lib/active_support/i18n.rb +1 -1
- data/lib/active_support/i18n_railtie.rb +20 -13
- data/lib/active_support/inflector/inflections.rb +2 -0
- data/lib/active_support/inflector/methods.rb +23 -11
- data/lib/active_support/inflector/transliterate.rb +3 -1
- data/lib/active_support/isolated_execution_state.rb +26 -22
- data/lib/active_support/json/decoding.rb +2 -1
- data/lib/active_support/json/encoding.rb +25 -43
- data/lib/active_support/key_generator.rb +9 -1
- data/lib/active_support/lazy_load_hooks.rb +6 -4
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber.rb +78 -33
- data/lib/active_support/logger.rb +1 -1
- data/lib/active_support/logger_thread_safe_level.rb +9 -22
- data/lib/active_support/message_encryptor.rb +197 -53
- data/lib/active_support/message_encryptors.rb +140 -0
- data/lib/active_support/message_pack/cache_serializer.rb +23 -0
- data/lib/active_support/message_pack/extensions.rb +292 -0
- data/lib/active_support/message_pack/serializer.rb +63 -0
- data/lib/active_support/message_pack.rb +50 -0
- data/lib/active_support/message_verifier.rb +212 -93
- data/lib/active_support/message_verifiers.rb +134 -0
- data/lib/active_support/messages/codec.rb +65 -0
- data/lib/active_support/messages/metadata.rb +111 -45
- data/lib/active_support/messages/rotation_coordinator.rb +93 -0
- data/lib/active_support/messages/rotator.rb +34 -32
- data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
- data/lib/active_support/multibyte/chars.rb +2 -0
- data/lib/active_support/multibyte/unicode.rb +9 -37
- data/lib/active_support/notifications/fanout.rb +239 -81
- data/lib/active_support/notifications/instrumenter.rb +79 -30
- data/lib/active_support/notifications.rb +1 -1
- data/lib/active_support/number_helper/number_converter.rb +5 -14
- data/lib/active_support/number_helper/number_to_currency_converter.rb +6 -6
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +3 -3
- data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -0
- data/lib/active_support/number_helper.rb +318 -379
- data/lib/active_support/ordered_hash.rb +3 -3
- data/lib/active_support/ordered_options.rb +14 -0
- data/lib/active_support/parameter_filter.rb +84 -69
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/railtie.rb +33 -21
- data/lib/active_support/reloader.rb +12 -4
- data/lib/active_support/rescuable.rb +2 -0
- data/lib/active_support/secure_compare_rotator.rb +16 -9
- data/lib/active_support/string_inquirer.rb +3 -1
- data/lib/active_support/subscriber.rb +9 -27
- data/lib/active_support/syntax_error_proxy.rb +49 -0
- data/lib/active_support/tagged_logging.rb +60 -24
- data/lib/active_support/test_case.rb +153 -6
- data/lib/active_support/testing/assertions.rb +26 -10
- data/lib/active_support/testing/autorun.rb +0 -2
- data/lib/active_support/testing/constant_stubbing.rb +32 -0
- data/lib/active_support/testing/deprecation.rb +25 -25
- data/lib/active_support/testing/error_reporter_assertions.rb +108 -0
- data/lib/active_support/testing/isolation.rb +1 -1
- data/lib/active_support/testing/method_call_assertions.rb +21 -8
- data/lib/active_support/testing/parallelize_executor.rb +8 -3
- data/lib/active_support/testing/stream.rb +1 -1
- data/lib/active_support/testing/strict_warnings.rb +38 -0
- data/lib/active_support/testing/time_helpers.rb +32 -14
- data/lib/active_support/time_with_zone.rb +4 -14
- data/lib/active_support/values/time_zone.rb +9 -7
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini/jdom.rb +3 -10
- data/lib/active_support/xml_mini/nokogiri.rb +1 -1
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -1
- data/lib/active_support/xml_mini/rexml.rb +1 -1
- data/lib/active_support/xml_mini.rb +2 -2
- data/lib/active_support.rb +13 -3
- metadata +48 -58
- data/lib/active_support/core_ext/array/deprecated_conversions.rb +0 -25
- data/lib/active_support/core_ext/date/deprecated_conversions.rb +0 -40
- data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +0 -36
- data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +0 -60
- data/lib/active_support/core_ext/range/deprecated_conversions.rb +0 -36
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +0 -5
- data/lib/active_support/core_ext/range/overlaps.rb +0 -36
- data/lib/active_support/core_ext/time/deprecated_conversions.rb +0 -73
- data/lib/active_support/core_ext/uri.rb +0 -5
- data/lib/active_support/per_thread_registry.rb +0 -65
|
@@ -18,438 +18,377 @@ module ActiveSupport
|
|
|
18
18
|
|
|
19
19
|
extend self
|
|
20
20
|
|
|
21
|
-
# Formats +number+ into a phone number.
|
|
22
|
-
#
|
|
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"
|
|
27
|
-
#
|
|
28
|
-
# number_to_phone(1235551234, delimiter: ".", country_code: 1, extension: 1343)
|
|
29
|
-
# # => "+1.123.555.1234 x 1343"
|
|
21
|
+
# Formats a +number+ into a phone number (US by default e.g., (555)
|
|
22
|
+
# 123-9876). You can customize the format in the +options+ hash.
|
|
30
23
|
#
|
|
31
24
|
# ==== Options
|
|
32
25
|
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
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"
|
|
26
|
+
# * <tt>:area_code</tt> - Adds parentheses around the area code.
|
|
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: '.')
|
|
47
|
+
# # => "+1.123.555.1234 x 1343"
|
|
65
48
|
#
|
|
49
|
+
# number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
|
|
50
|
+
# # => "(755) 6123-4567"
|
|
51
|
+
# number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
|
|
52
|
+
# # => "133-1234-5678"
|
|
66
53
|
def number_to_phone(number, options = {})
|
|
67
54
|
NumberToPhoneConverter.convert(number, options)
|
|
68
55
|
end
|
|
69
56
|
|
|
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"
|
|
57
|
+
# Formats a +number+ into a currency string (e.g., $13.65). You
|
|
58
|
+
# can customize the format in the +options+ hash.
|
|
78
59
|
#
|
|
79
60
|
# The currency unit and number formatting of the current locale will be used
|
|
80
|
-
# unless otherwise specified
|
|
81
|
-
# performed. If the user is given a way to change their locale, they will
|
|
61
|
+
# unless otherwise specified in the provided options. No currency conversion
|
|
62
|
+
# is performed. If the user is given a way to change their locale, they will
|
|
82
63
|
# also be able to change the relative value of the currency displayed with
|
|
83
64
|
# this helper. If your application will ever support multiple locales, you
|
|
84
|
-
# may want to specify a constant
|
|
85
|
-
# library capable of currency conversion.
|
|
65
|
+
# may want to specify a constant <tt>:locale</tt> option or consider
|
|
66
|
+
# using a library capable of currency conversion.
|
|
86
67
|
#
|
|
87
68
|
# ==== Options
|
|
88
69
|
#
|
|
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
|
-
#
|
|
114
|
-
#
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
#
|
|
118
|
-
#
|
|
119
|
-
#
|
|
120
|
-
#
|
|
121
|
-
#
|
|
122
|
-
#
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
#
|
|
126
|
-
#
|
|
127
|
-
#
|
|
128
|
-
#
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
#
|
|
132
|
-
#
|
|
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
|
-
#
|
|
70
|
+
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
|
71
|
+
# (defaults to current locale).
|
|
72
|
+
# * <tt>:precision</tt> - Sets the level of precision (defaults
|
|
73
|
+
# to 2).
|
|
74
|
+
# * <tt>:round_mode</tt> - Determine how rounding is performed
|
|
75
|
+
# (defaults to :default. See BigDecimal::mode)
|
|
76
|
+
# * <tt>:unit</tt> - Sets the denomination of the currency
|
|
77
|
+
# (defaults to "$").
|
|
78
|
+
# * <tt>:separator</tt> - Sets the separator between the units
|
|
79
|
+
# (defaults to ".").
|
|
80
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
|
81
|
+
# to ",").
|
|
82
|
+
# * <tt>:format</tt> - Sets the format for non-negative numbers
|
|
83
|
+
# (defaults to "%u%n"). Fields are <tt>%u</tt> for the
|
|
84
|
+
# currency, and <tt>%n</tt> for the number.
|
|
85
|
+
# * <tt>:negative_format</tt> - Sets the format for negative
|
|
86
|
+
# numbers (defaults to prepending a hyphen to the formatted
|
|
87
|
+
# number given by <tt>:format</tt>). Accepts the same fields
|
|
88
|
+
# than <tt>:format</tt>, except <tt>%n</tt> is here the
|
|
89
|
+
# absolute value of the number.
|
|
90
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
|
91
|
+
# insignificant zeros after the decimal separator (defaults to
|
|
92
|
+
# +false+).
|
|
93
|
+
#
|
|
94
|
+
# ==== Examples
|
|
95
|
+
#
|
|
96
|
+
# number_to_currency(1234567890.50) # => "$1,234,567,890.50"
|
|
97
|
+
# number_to_currency(1234567890.506) # => "$1,234,567,890.51"
|
|
98
|
+
# number_to_currency(1234567890.506, precision: 3) # => "$1,234,567,890.506"
|
|
99
|
+
# number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
|
|
100
|
+
# number_to_currency('123a456') # => "$123a456"
|
|
101
|
+
#
|
|
102
|
+
# number_to_currency(-0.456789, precision: 0)
|
|
103
|
+
# # => "$0"
|
|
104
|
+
# number_to_currency(-1234567890.50, negative_format: '(%u%n)')
|
|
105
|
+
# # => "($1,234,567,890.50)"
|
|
106
|
+
# number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '')
|
|
107
|
+
# # => "£1234567890,50"
|
|
108
|
+
# number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '', format: '%n %u')
|
|
109
|
+
# # => "1234567890,50 £"
|
|
110
|
+
# number_to_currency(1234567890.50, strip_insignificant_zeros: true)
|
|
111
|
+
# # => "$1,234,567,890.5"
|
|
112
|
+
# number_to_currency(1234567890.50, precision: 0, round_mode: :up)
|
|
113
|
+
# # => "$1,234,567,891"
|
|
139
114
|
def number_to_currency(number, options = {})
|
|
140
115
|
NumberToCurrencyConverter.convert(number, options)
|
|
141
116
|
end
|
|
142
117
|
|
|
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%"
|
|
118
|
+
# Formats a +number+ as a percentage string (e.g., 65%). You can
|
|
119
|
+
# customize the format in the +options+ hash.
|
|
151
120
|
#
|
|
152
121
|
# ==== Options
|
|
153
122
|
#
|
|
154
|
-
#
|
|
155
|
-
#
|
|
156
|
-
#
|
|
157
|
-
#
|
|
158
|
-
#
|
|
159
|
-
#
|
|
160
|
-
#
|
|
161
|
-
#
|
|
162
|
-
#
|
|
163
|
-
#
|
|
164
|
-
#
|
|
165
|
-
#
|
|
166
|
-
#
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
#
|
|
172
|
-
#
|
|
173
|
-
#
|
|
174
|
-
#
|
|
175
|
-
#
|
|
176
|
-
#
|
|
177
|
-
#
|
|
178
|
-
#
|
|
179
|
-
#
|
|
180
|
-
#
|
|
181
|
-
#
|
|
182
|
-
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
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
|
-
#
|
|
123
|
+
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
|
124
|
+
# (defaults to current locale).
|
|
125
|
+
# * <tt>:precision</tt> - Sets the precision of the number
|
|
126
|
+
# (defaults to 3). Keeps the number's precision if +nil+.
|
|
127
|
+
# * <tt>:round_mode</tt> - Determine how rounding is performed
|
|
128
|
+
# (defaults to :default. See BigDecimal::mode)
|
|
129
|
+
# * <tt>:significant</tt> - If +true+, precision will be the number
|
|
130
|
+
# of significant_digits. If +false+, the number of fractional
|
|
131
|
+
# digits (defaults to +false+).
|
|
132
|
+
# * <tt>:separator</tt> - Sets the separator between the
|
|
133
|
+
# fractional and integer digits (defaults to ".").
|
|
134
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
|
135
|
+
# to "").
|
|
136
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
|
137
|
+
# insignificant zeros after the decimal separator (defaults to
|
|
138
|
+
# +false+).
|
|
139
|
+
# * <tt>:format</tt> - Specifies the format of the percentage
|
|
140
|
+
# string The number field is <tt>%n</tt> (defaults to "%n%").
|
|
141
|
+
#
|
|
142
|
+
# ==== Examples
|
|
143
|
+
#
|
|
144
|
+
# number_to_percentage(100) # => "100.000%"
|
|
145
|
+
# number_to_percentage('98') # => "98.000%"
|
|
146
|
+
# number_to_percentage(100, precision: 0) # => "100%"
|
|
147
|
+
# number_to_percentage(1000, delimiter: '.', separator: ',') # => "1.000,000%"
|
|
148
|
+
# number_to_percentage(302.24398923423, precision: 5) # => "302.24399%"
|
|
149
|
+
# number_to_percentage(1000, locale: :fr) # => "1000,000%"
|
|
150
|
+
# number_to_percentage(1000, precision: nil) # => "1000%"
|
|
151
|
+
# number_to_percentage('98a') # => "98a%"
|
|
152
|
+
# number_to_percentage(100, format: '%n %') # => "100.000 %"
|
|
153
|
+
# number_to_percentage(302.24398923423, precision: 5, round_mode: :down) # => "302.24398%"
|
|
201
154
|
def number_to_percentage(number, options = {})
|
|
202
155
|
NumberToPercentageConverter.convert(number, options)
|
|
203
156
|
end
|
|
204
157
|
|
|
205
|
-
# Formats +number+
|
|
206
|
-
#
|
|
207
|
-
#
|
|
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"
|
|
158
|
+
# Formats a +number+ with grouped thousands using +delimiter+
|
|
159
|
+
# (e.g., 12,324). You can customize the format in the +options+
|
|
160
|
+
# hash.
|
|
214
161
|
#
|
|
215
162
|
# ==== Options
|
|
216
163
|
#
|
|
217
|
-
#
|
|
218
|
-
#
|
|
219
|
-
#
|
|
220
|
-
#
|
|
221
|
-
#
|
|
222
|
-
#
|
|
223
|
-
#
|
|
224
|
-
#
|
|
225
|
-
#
|
|
226
|
-
#
|
|
227
|
-
#
|
|
228
|
-
#
|
|
229
|
-
#
|
|
230
|
-
#
|
|
231
|
-
#
|
|
232
|
-
#
|
|
233
|
-
#
|
|
234
|
-
#
|
|
235
|
-
#
|
|
236
|
-
#
|
|
237
|
-
#
|
|
238
|
-
#
|
|
239
|
-
#
|
|
240
|
-
#
|
|
241
|
-
#
|
|
164
|
+
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
|
165
|
+
# (defaults to current locale).
|
|
166
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
|
167
|
+
# to ",").
|
|
168
|
+
# * <tt>:separator</tt> - Sets the separator between the
|
|
169
|
+
# fractional and integer digits (defaults to ".").
|
|
170
|
+
# * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
|
|
171
|
+
# deriving the placement of delimiter. Helpful when using currency formats
|
|
172
|
+
# like INR.
|
|
173
|
+
#
|
|
174
|
+
# ==== Examples
|
|
175
|
+
#
|
|
176
|
+
# number_to_delimited(12345678) # => "12,345,678"
|
|
177
|
+
# number_to_delimited('123456') # => "123,456"
|
|
178
|
+
# number_to_delimited(12345678.05) # => "12,345,678.05"
|
|
179
|
+
# number_to_delimited(12345678, delimiter: '.') # => "12.345.678"
|
|
180
|
+
# number_to_delimited(12345678, delimiter: ',') # => "12,345,678"
|
|
181
|
+
# number_to_delimited(12345678.05, separator: ' ') # => "12,345,678 05"
|
|
182
|
+
# number_to_delimited(12345678.05, locale: :fr) # => "12 345 678,05"
|
|
183
|
+
# number_to_delimited('112a') # => "112a"
|
|
184
|
+
# number_to_delimited(98765432.98, delimiter: ' ', separator: ',')
|
|
185
|
+
# # => "98 765 432,98"
|
|
186
|
+
# number_to_delimited("123456.78",
|
|
187
|
+
# delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
|
|
188
|
+
# # => "1,23,456.78"
|
|
242
189
|
def number_to_delimited(number, options = {})
|
|
243
190
|
NumberToDelimitedConverter.convert(number, options)
|
|
244
191
|
end
|
|
245
192
|
|
|
246
|
-
# Formats +number+
|
|
247
|
-
#
|
|
248
|
-
#
|
|
249
|
-
#
|
|
250
|
-
# number_to_rounded(12345.6789, precision: 0) # => "12345"
|
|
251
|
-
# number_to_rounded(12345, precision: 5) # => "12345.00000"
|
|
193
|
+
# Formats a +number+ with the specified level of
|
|
194
|
+
# <tt>:precision</tt> (e.g., 112.32 has a precision of 2 if
|
|
195
|
+
# +:significant+ is +false+, and 5 if +:significant+ is +true+).
|
|
196
|
+
# You can customize the format in the +options+ hash.
|
|
252
197
|
#
|
|
253
198
|
# ==== Options
|
|
254
199
|
#
|
|
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
|
-
#
|
|
283
|
-
#
|
|
284
|
-
#
|
|
285
|
-
#
|
|
286
|
-
#
|
|
287
|
-
#
|
|
288
|
-
#
|
|
289
|
-
#
|
|
290
|
-
#
|
|
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
|
-
#
|
|
200
|
+
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
|
201
|
+
# (defaults to current locale).
|
|
202
|
+
# * <tt>:precision</tt> - Sets the precision of the number
|
|
203
|
+
# (defaults to 3). Keeps the number's precision if +nil+.
|
|
204
|
+
# * <tt>:round_mode</tt> - Determine how rounding is performed
|
|
205
|
+
# (defaults to :default. See BigDecimal::mode)
|
|
206
|
+
# * <tt>:significant</tt> - If +true+, precision will be the number
|
|
207
|
+
# of significant_digits. If +false+, the number of fractional
|
|
208
|
+
# digits (defaults to +false+).
|
|
209
|
+
# * <tt>:separator</tt> - Sets the separator between the
|
|
210
|
+
# fractional and integer digits (defaults to ".").
|
|
211
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
|
212
|
+
# to "").
|
|
213
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
|
214
|
+
# insignificant zeros after the decimal separator (defaults to
|
|
215
|
+
# +false+).
|
|
216
|
+
#
|
|
217
|
+
# ==== Examples
|
|
218
|
+
#
|
|
219
|
+
# number_to_rounded(111.2345) # => "111.235"
|
|
220
|
+
# number_to_rounded(111.2345, precision: 2) # => "111.23"
|
|
221
|
+
# number_to_rounded(13, precision: 5) # => "13.00000"
|
|
222
|
+
# number_to_rounded(389.32314, precision: 0) # => "389"
|
|
223
|
+
# number_to_rounded(111.2345, significant: true) # => "111"
|
|
224
|
+
# number_to_rounded(111.2345, precision: 1, significant: true) # => "100"
|
|
225
|
+
# number_to_rounded(13, precision: 5, significant: true) # => "13.000"
|
|
226
|
+
# number_to_rounded(13, precision: nil) # => "13"
|
|
227
|
+
# number_to_rounded(389.32314, precision: 0, round_mode: :up) # => "390"
|
|
228
|
+
# number_to_rounded(111.234, locale: :fr) # => "111,234"
|
|
229
|
+
#
|
|
230
|
+
# number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
|
|
231
|
+
# # => "13"
|
|
232
|
+
#
|
|
233
|
+
# number_to_rounded(389.32314, precision: 4, significant: true) # => "389.3"
|
|
234
|
+
# number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.')
|
|
235
|
+
# # => "1.111,23"
|
|
298
236
|
def number_to_rounded(number, options = {})
|
|
299
237
|
NumberToRoundedConverter.convert(number, options)
|
|
300
238
|
end
|
|
301
239
|
|
|
302
|
-
# Formats +number+
|
|
303
|
-
#
|
|
304
|
-
#
|
|
305
|
-
#
|
|
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"
|
|
240
|
+
# Formats the bytes in +number+ into a more understandable
|
|
241
|
+
# representation (e.g., giving it 1500 yields 1.46 KB). This
|
|
242
|
+
# method is useful for reporting file sizes to users. You can
|
|
243
|
+
# customize the format in the +options+ hash.
|
|
313
244
|
#
|
|
314
|
-
# See
|
|
245
|
+
# See <tt>number_to_human</tt> if you want to pretty-print a
|
|
246
|
+
# generic number.
|
|
315
247
|
#
|
|
316
248
|
# ==== Options
|
|
317
249
|
#
|
|
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
|
-
#
|
|
250
|
+
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
|
251
|
+
# (defaults to current locale).
|
|
252
|
+
# * <tt>:precision</tt> - Sets the precision of the number
|
|
253
|
+
# (defaults to 3).
|
|
254
|
+
# * <tt>:round_mode</tt> - Determine how rounding is performed
|
|
255
|
+
# (defaults to :default. See BigDecimal::mode)
|
|
256
|
+
# * <tt>:significant</tt> - If +true+, precision will be the number
|
|
257
|
+
# of significant_digits. If +false+, the number of fractional
|
|
258
|
+
# digits (defaults to +true+)
|
|
259
|
+
# * <tt>:separator</tt> - Sets the separator between the
|
|
260
|
+
# fractional and integer digits (defaults to ".").
|
|
261
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
|
262
|
+
# to "").
|
|
263
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
|
264
|
+
# insignificant zeros after the decimal separator (defaults to
|
|
265
|
+
# +true+)
|
|
266
|
+
#
|
|
267
|
+
# ==== Examples
|
|
268
|
+
#
|
|
269
|
+
# number_to_human_size(123) # => "123 Bytes"
|
|
270
|
+
# number_to_human_size(1234) # => "1.21 KB"
|
|
271
|
+
# number_to_human_size(12345) # => "12.1 KB"
|
|
272
|
+
# number_to_human_size(1234567) # => "1.18 MB"
|
|
273
|
+
# number_to_human_size(1234567890) # => "1.15 GB"
|
|
274
|
+
# number_to_human_size(1234567890123) # => "1.12 TB"
|
|
275
|
+
# number_to_human_size(1234567890123456) # => "1.1 PB"
|
|
276
|
+
# number_to_human_size(1234567890123456789) # => "1.07 EB"
|
|
277
|
+
# number_to_human_size(1234567, precision: 2) # => "1.2 MB"
|
|
278
|
+
# number_to_human_size(483989, precision: 2) # => "470 KB"
|
|
279
|
+
# number_to_human_size(483989, precision: 2, round_mode: :up) # => "480 KB"
|
|
280
|
+
# number_to_human_size(1234567, precision: 2, separator: ',') # => "1,2 MB"
|
|
281
|
+
# number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
|
|
282
|
+
# number_to_human_size(524288000, precision: 5) # => "500 MB"
|
|
351
283
|
def number_to_human_size(number, options = {})
|
|
352
284
|
NumberToHumanSizeConverter.convert(number, options)
|
|
353
285
|
end
|
|
354
286
|
|
|
355
|
-
#
|
|
356
|
-
#
|
|
287
|
+
# Pretty prints (formats and approximates) a number in a way it
|
|
288
|
+
# is more readable by humans (e.g.: 1200000000 becomes "1.2
|
|
289
|
+
# Billion"). This is useful for numbers that can get very large
|
|
290
|
+
# (and too hard to read).
|
|
357
291
|
#
|
|
358
|
-
#
|
|
359
|
-
#
|
|
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"
|
|
292
|
+
# See <tt>number_to_human_size</tt> if you want to print a file
|
|
293
|
+
# size.
|
|
366
294
|
#
|
|
367
|
-
#
|
|
295
|
+
# You can also define your own unit-quantifier names if you want
|
|
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).
|
|
368
300
|
#
|
|
369
301
|
# ==== Options
|
|
370
302
|
#
|
|
371
|
-
#
|
|
372
|
-
#
|
|
373
|
-
#
|
|
374
|
-
#
|
|
375
|
-
#
|
|
376
|
-
#
|
|
377
|
-
#
|
|
378
|
-
#
|
|
379
|
-
#
|
|
380
|
-
#
|
|
381
|
-
#
|
|
382
|
-
#
|
|
383
|
-
#
|
|
384
|
-
#
|
|
385
|
-
#
|
|
386
|
-
#
|
|
387
|
-
#
|
|
388
|
-
#
|
|
389
|
-
#
|
|
390
|
-
#
|
|
391
|
-
#
|
|
392
|
-
#
|
|
393
|
-
#
|
|
394
|
-
#
|
|
395
|
-
#
|
|
396
|
-
#
|
|
397
|
-
#
|
|
398
|
-
#
|
|
399
|
-
#
|
|
400
|
-
#
|
|
401
|
-
#
|
|
402
|
-
#
|
|
403
|
-
#
|
|
404
|
-
#
|
|
405
|
-
#
|
|
406
|
-
#
|
|
407
|
-
#
|
|
408
|
-
#
|
|
409
|
-
#
|
|
410
|
-
#
|
|
411
|
-
#
|
|
412
|
-
#
|
|
413
|
-
#
|
|
414
|
-
#
|
|
415
|
-
#
|
|
416
|
-
#
|
|
417
|
-
#
|
|
418
|
-
#
|
|
419
|
-
#
|
|
420
|
-
#
|
|
421
|
-
#
|
|
422
|
-
#
|
|
423
|
-
#
|
|
424
|
-
#
|
|
425
|
-
#
|
|
426
|
-
#
|
|
427
|
-
#
|
|
428
|
-
#
|
|
429
|
-
#
|
|
430
|
-
#
|
|
431
|
-
#
|
|
432
|
-
#
|
|
433
|
-
#
|
|
434
|
-
#
|
|
435
|
-
#
|
|
436
|
-
#
|
|
437
|
-
#
|
|
438
|
-
#
|
|
439
|
-
#
|
|
440
|
-
#
|
|
441
|
-
#
|
|
442
|
-
#
|
|
443
|
-
#
|
|
444
|
-
#
|
|
445
|
-
#
|
|
446
|
-
#
|
|
447
|
-
#
|
|
448
|
-
#
|
|
449
|
-
#
|
|
450
|
-
#
|
|
451
|
-
#
|
|
452
|
-
#
|
|
303
|
+
# * <tt>:locale</tt> - Sets the locale to be used for formatting
|
|
304
|
+
# (defaults to current locale).
|
|
305
|
+
# * <tt>:precision</tt> - Sets the precision of the number
|
|
306
|
+
# (defaults to 3).
|
|
307
|
+
# * <tt>:round_mode</tt> - Determine how rounding is performed
|
|
308
|
+
# (defaults to :default. See BigDecimal::mode)
|
|
309
|
+
# * <tt>:significant</tt> - If +true+, precision will be the number
|
|
310
|
+
# of significant_digits. If +false+, the number of fractional
|
|
311
|
+
# digits (defaults to +true+)
|
|
312
|
+
# * <tt>:separator</tt> - Sets the separator between the
|
|
313
|
+
# fractional and integer digits (defaults to ".").
|
|
314
|
+
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults
|
|
315
|
+
# to "").
|
|
316
|
+
# * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
|
|
317
|
+
# insignificant zeros after the decimal separator (defaults to
|
|
318
|
+
# +true+)
|
|
319
|
+
# * <tt>:units</tt> - A Hash of unit quantifier names. Or a
|
|
320
|
+
# string containing an i18n scope where to find this hash. It
|
|
321
|
+
# might have the following keys:
|
|
322
|
+
# * *integers*: <tt>:unit</tt>, <tt>:ten</tt>,
|
|
323
|
+
# <tt>:hundred</tt>, <tt>:thousand</tt>, <tt>:million</tt>,
|
|
324
|
+
# <tt>:billion</tt>, <tt>:trillion</tt>,
|
|
325
|
+
# <tt>:quadrillion</tt>
|
|
326
|
+
# * *fractionals*: <tt>:deci</tt>, <tt>:centi</tt>,
|
|
327
|
+
# <tt>:mili</tt>, <tt>:micro</tt>, <tt>:nano</tt>,
|
|
328
|
+
# <tt>:pico</tt>, <tt>:femto</tt>
|
|
329
|
+
# * <tt>:format</tt> - Sets the format of the output string
|
|
330
|
+
# (defaults to "%n %u"). The field types are:
|
|
331
|
+
# * %u - The quantifier (ex.: 'thousand')
|
|
332
|
+
# * %n - The number
|
|
333
|
+
#
|
|
334
|
+
# ==== Examples
|
|
335
|
+
#
|
|
336
|
+
# number_to_human(123) # => "123"
|
|
337
|
+
# number_to_human(1234) # => "1.23 Thousand"
|
|
338
|
+
# number_to_human(12345) # => "12.3 Thousand"
|
|
339
|
+
# number_to_human(1234567) # => "1.23 Million"
|
|
340
|
+
# number_to_human(1234567890) # => "1.23 Billion"
|
|
341
|
+
# number_to_human(1234567890123) # => "1.23 Trillion"
|
|
342
|
+
# number_to_human(1234567890123456) # => "1.23 Quadrillion"
|
|
343
|
+
# number_to_human(1234567890123456789) # => "1230 Quadrillion"
|
|
344
|
+
# number_to_human(489939, precision: 2) # => "490 Thousand"
|
|
345
|
+
# number_to_human(489939, precision: 4) # => "489.9 Thousand"
|
|
346
|
+
# number_to_human(489939, precision: 2
|
|
347
|
+
# , round_mode: :down) # => "480 Thousand"
|
|
348
|
+
# number_to_human(1234567, precision: 4,
|
|
349
|
+
# significant: false) # => "1.2346 Million"
|
|
350
|
+
# number_to_human(1234567, precision: 1,
|
|
351
|
+
# separator: ',',
|
|
352
|
+
# significant: false) # => "1,2 Million"
|
|
353
|
+
#
|
|
354
|
+
# number_to_human(500000000, precision: 5) # => "500 Million"
|
|
355
|
+
# number_to_human(12345012345, significant: false) # => "12.345 Billion"
|
|
356
|
+
#
|
|
357
|
+
# Non-significant zeros after the decimal separator are stripped
|
|
358
|
+
# out by default (set <tt>:strip_insignificant_zeros</tt> to
|
|
359
|
+
# +false+ to change that):
|
|
360
|
+
#
|
|
361
|
+
# number_to_human(12.00001) # => "12"
|
|
362
|
+
# number_to_human(12.00001, strip_insignificant_zeros: false) # => "12.0"
|
|
363
|
+
#
|
|
364
|
+
# ==== Custom Unit Quantifiers
|
|
365
|
+
#
|
|
366
|
+
# You can also use your own custom unit quantifiers:
|
|
367
|
+
#
|
|
368
|
+
# number_to_human(500000, units: { unit: 'ml', thousand: 'lt' }) # => "500 lt"
|
|
369
|
+
#
|
|
370
|
+
# If in your I18n locale you have:
|
|
371
|
+
#
|
|
372
|
+
# distance:
|
|
373
|
+
# centi:
|
|
374
|
+
# one: "centimeter"
|
|
375
|
+
# other: "centimeters"
|
|
376
|
+
# unit:
|
|
377
|
+
# one: "meter"
|
|
378
|
+
# other: "meters"
|
|
379
|
+
# thousand:
|
|
380
|
+
# one: "kilometer"
|
|
381
|
+
# other: "kilometers"
|
|
382
|
+
# billion: "gazillion-distance"
|
|
383
|
+
#
|
|
384
|
+
# Then you could do:
|
|
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"
|
|
453
392
|
def number_to_human(number, options = {})
|
|
454
393
|
NumberToHumanConverter.convert(number, options)
|
|
455
394
|
end
|