Permalink
Browse files

Update to the helper to abstract delimeting a number #1015

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1111 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 7ad57c4 commit 4bdb25da181a8a7acd8956fe53a244224a08be21 @dhh dhh committed Apr 7, 2005
Showing with 10 additions and 4 deletions.
  1. +10 −4 actionpack/lib/action_view/helpers/number_helper.rb
@@ -35,14 +35,13 @@ def number_to_phone(number, options = {})
# Examples:
# number_to_currency(1234567890.50) => $1,234,567,890.50
# number_to_currency(1234567890.506) => $1,234,567,890.51
- # number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delim => ""}) => £123456789,50
+ # number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimeter => ""}) => £123456789,50
def number_to_currency(number, options = {})
options = options.stringify_keys
- precision, unit, separator, delim = options.delete("precision") { 2 }, options.delete("unit") { "$" }, options.delete("separator") { "." }, options.delete("delim") { "," }
+ precision, unit, separator, delimeter = options.delete("precision") { 2 }, options.delete("unit") { "$" }, options.delete("separator") { "." }, options.delete("delimeter") { "," }
begin
parts = number_with_precision(number, precision).split('.')
- parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delim}")
- unit + parts[0] + separator + parts[1].to_s
+ unit + number_with_delimeter(parts[0]) + separator + parts[1].to_s
rescue
number
end
@@ -71,6 +70,13 @@ def number_to_percentage(number, options = {})
end
end
+ # Formats a +number+ with a +delimeter+.
+ # Example:
+ # number_with_delimeter(12345678) => 1,235,678
+ def number_with_delimeter(number, delimeter=",")
+ number.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimeter}"
+ end
+
# Formats a +number+ with a level of +precision+.
# Example:
# number_with_precision(111.2345) => 111.235

0 comments on commit 4bdb25d

Please sign in to comment.