Skip to content
This repository
Browse code

Added :format option to NumberHelper#number_to_currency to enable bet…

…ter localization support #11149 [lylo]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9052 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3c0fd445c0a36c36cbd8c4259a28a978a8e8eb83 1 parent 75d98db
David Heinemeier Hansson authored March 17, 2008
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Added :format option to NumberHelper#number_to_currency to enable better localization support #11149 [lylo]
  4
+
3 5
 * Fixed that TextHelper#excerpt would include one character too many #11268 [Irfy]
4 6
 
5 7
 * Fix more obscure nested parameter hash parsing bug.  #10797 [thomas.lee]
9  actionpack/lib/action_view/helpers/number_helper.rb
@@ -54,6 +54,10 @@ def number_to_phone(number, options = {})
54 54
       # * <tt>:unit</tt>  - Sets the denomination of the currency (defaults to "$").
55 55
       # * <tt>:separator</tt>  - Sets the separator between the units (defaults to ".").
56 56
       # * <tt>:delimiter</tt>  - Sets the thousands delimiter (defaults to ",").
  57
+      # * <tt>:format</tt>  - Sets the format of the output string (defaults to "%u%n"). The field types are:
  58
+      #
  59
+      #     %u  The currency unit
  60
+      #     %n  The number
57 61
       #
58 62
       # ==== Examples
59 63
       #  number_to_currency(1234567890.50)                    # => $1,234,567,890.50
@@ -62,16 +66,19 @@ def number_to_phone(number, options = {})
62 66
       #
63 67
       #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "")
64 68
       #  # => &pound;1234567890,50
  69
+      #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
  70
+      #  # => 1234567890,50 &pound;
65 71
       def number_to_currency(number, options = {})
66 72
         options   = options.stringify_keys
67 73
         precision = options["precision"] || 2
68 74
         unit      = options["unit"] || "$"
69 75
         separator = precision > 0 ? options["separator"] || "." : ""
70 76
         delimiter = options["delimiter"] || ","
  77
+        format    = options["format"] || "%u%n"
71 78
 
72 79
         begin
73 80
           parts = number_with_precision(number, precision).split('.')
74  
-          unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s
  81
+          format.gsub(/%n/, number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s).gsub(/%u/, unit)
75 82
         rescue
76 83
           number
77 84
         end
1  actionpack/test/template/number_helper_test.rb
@@ -25,6 +25,7 @@ def test_number_to_currency
25 25
     assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1}))
26 26
     assert_equal("&pound;1234567890,50", number_to_currency(1234567890.50, {:unit => "&pound;", :separator => ",", :delimiter => ""}))
27 27
     assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50"))
  28
+    assert_equal("1,234,567,890.50 K&#269;", number_to_currency("1234567890.50", {:unit => "K&#269;", :format => "%n %u"}))
28 29
     assert_equal("$x.", number_to_currency("x"))
29 30
     assert_nil number_to_currency(nil)
30 31
   end

0 notes on commit 3c0fd44

Please sign in to comment.
Something went wrong with that request. Please try again.