aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/number_helper.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-03-17 22:08:25 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-03-17 22:08:25 +0000
commit3c0fd445c0a36c36cbd8c4259a28a978a8e8eb83 (patch)
tree1beb8324cbf9349d7832e76838bc0d0f30065d0a /actionpack/lib/action_view/helpers/number_helper.rb
parent75d98db73d053641d24a7356345766b0a7659de0 (diff)
downloadrails-3c0fd445c0a36c36cbd8c4259a28a978a8e8eb83.tar.gz
rails-3c0fd445c0a36c36cbd8c4259a28a978a8e8eb83.tar.bz2
rails-3c0fd445c0a36c36cbd8c4259a28a978a8e8eb83.zip
Added :format option to NumberHelper#number_to_currency to enable better localization support #11149 [lylo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9052 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers/number_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/number_helper.rb9
1 files changed, 8 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb
index 7dcba4a33e..fe40725402 100644
--- a/actionpack/lib/action_view/helpers/number_helper.rb
+++ b/actionpack/lib/action_view/helpers/number_helper.rb
@@ -54,6 +54,10 @@ module ActionView
# * <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
@@ -62,16 +66,19 @@ module ActionView
#
# 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