Skip to content

Commit

Permalink
Added :format option to NumberHelper#number_to_currency to enable bet…
Browse files Browse the repository at this point in the history
…ter localization support #11149 [lylo]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9052 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Mar 17, 2008
1 parent 75d98db commit 3c0fd44
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Added :format option to NumberHelper#number_to_currency to enable better localization support #11149 [lylo]

* Fixed that TextHelper#excerpt would include one character too many #11268 [Irfy]

* Fix more obscure nested parameter hash parsing bug. #10797 [thomas.lee]
Expand Down
9 changes: 8 additions & 1 deletion actionpack/lib/action_view/helpers/number_helper.rb
Expand Up @@ -54,6 +54,10 @@ def number_to_phone(number, options = {})
# * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$").
# * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
# * <tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are:
#
# %u The currency unit
# %n The number
#
# ==== Examples
# number_to_currency(1234567890.50) # => $1,234,567,890.50
Expand All @@ -62,16 +66,19 @@ def number_to_phone(number, options = {})
#
# number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "")
# # => &pound;1234567890,50
# number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
# # => 1234567890,50 &pound;
def number_to_currency(number, options = {})
options = options.stringify_keys
precision = options["precision"] || 2
unit = options["unit"] || "$"
separator = precision > 0 ? options["separator"] || "." : ""
delimiter = options["delimiter"] || ","
format = options["format"] || "%u%n"

begin
parts = number_with_precision(number, precision).split('.')
unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s
format.gsub(/%n/, number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s).gsub(/%u/, unit)
rescue
number
end
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/template/number_helper_test.rb
Expand Up @@ -25,6 +25,7 @@ def test_number_to_currency
assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1}))
assert_equal("&pound;1234567890,50", number_to_currency(1234567890.50, {:unit => "&pound;", :separator => ",", :delimiter => ""}))
assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50"))
assert_equal("1,234,567,890.50 K&#269;", number_to_currency("1234567890.50", {:unit => "K&#269;", :format => "%n %u"}))
assert_equal("$x.", number_to_currency("x"))
assert_nil number_to_currency(nil)
end
Expand Down

0 comments on commit 3c0fd44

Please sign in to comment.