aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb')
-rw-r--r--activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb21
1 files changed, 21 insertions, 0 deletions
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
new file mode 100644
index 0000000000..6405afc9a6
--- /dev/null
+++ b/activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb
@@ -0,0 +1,21 @@
+module ActiveSupport
+ module NumberHelper
+ class NumberToDelimitedConverter < NumberConverter #:nodoc:
+ self.validate_float = true
+
+ DELIMITED_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/
+
+ def convert
+ parts.join(options[:separator])
+ end
+
+ private
+
+ def parts
+ left, right = number.to_s.split('.')
+ left.gsub!(DELIMITED_REGEX) { "#{$1}#{options[:delimiter]}" }
+ [left, right].compact
+ end
+ end
+ end
+end