diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-28 03:07:34 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-28 03:07:34 -0300 |
commit | 93a4dfaebc48e3324997be780539fad996349c84 (patch) | |
tree | f889ee6897b455120356f64505579c5864d15dd3 /activesupport/lib | |
parent | 3cc7b0a239c66af39acc979d9afa9e7c593c1f4b (diff) | |
parent | 7f23c5d52438f12d7af167a399a77025b1d58a9c (diff) | |
download | rails-93a4dfaebc48e3324997be780539fad996349c84.tar.gz rails-93a4dfaebc48e3324997be780539fad996349c84.tar.bz2 rails-93a4dfaebc48e3324997be780539fad996349c84.zip |
Merge pull request #17502 from vipulnsward/configurable-delimited-regex
Support for custom regular expression for number_to_delimeted
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/number_helper.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb | 9 |
2 files changed, 12 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/number_helper.rb b/activesupport/lib/active_support/number_helper.rb index 38a9ce361d..5ab0f0a7ea 100644 --- a/activesupport/lib/active_support/number_helper.rb +++ b/activesupport/lib/active_support/number_helper.rb @@ -135,6 +135,9 @@ module ActiveSupport # to ","). # * <tt>:separator</tt> - Sets the separator between the # fractional and integer digits (defaults to "."). + # * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for + # for deriving, the placement of delimiter. Helpful when using currency + # formats like INR. # # ==== Examples # @@ -147,6 +150,8 @@ module ActiveSupport # number_to_delimited(12345678.05, locale: :fr) # => 12 345 678,05 # number_to_delimited('112a') # => 112a # number_to_delimited(98765432.98, delimiter: ' ', separator: ',') + # number_helper.number_to_delimited("123456.78", + # delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/) # => 1,23,456.78 # # => 98 765 432,98 def number_to_delimited(number, options = {}) NumberToDelimitedConverter.convert(number, options) diff --git a/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb b/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb index d85cc086d7..45ae8f1a93 100644 --- a/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb +++ b/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb @@ -3,7 +3,7 @@ module ActiveSupport class NumberToDelimitedConverter < NumberConverter #:nodoc: self.validate_float = true - DELIMITED_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/ + DEFAULT_DELIMITER_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/ def convert parts.join(options[:separator]) @@ -13,11 +13,16 @@ module ActiveSupport def parts left, right = number.to_s.split('.') - left.gsub!(DELIMITED_REGEX) do |digit_to_delimit| + left.gsub!(delimiter_pattern) do |digit_to_delimit| "#{digit_to_delimit}#{options[:delimiter]}" end [left, right].compact end + + def delimiter_pattern + options.fetch(:delimiter_pattern, DEFAULT_DELIMITER_REGEX) + end + end end end |