aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/number_helper/number_to_delimited.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/number_helper/number_to_delimited.rb')
-rw-r--r--activesupport/lib/active_support/number_helper/number_to_delimited.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/number_helper/number_to_delimited.rb b/activesupport/lib/active_support/number_helper/number_to_delimited.rb
new file mode 100644
index 0000000000..b543b0c19d
--- /dev/null
+++ b/activesupport/lib/active_support/number_helper/number_to_delimited.rb
@@ -0,0 +1,25 @@
+require 'active_support/number_helper/number_converter'
+
+module ActiveSupport
+ module NumberHelper
+ class NumberToDelimitedConverter < NumberConverter #:nodoc:
+
+ self.need_valid_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