From 3c0fd445c0a36c36cbd8c4259a28a978a8e8eb83 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 17 Mar 2008 22:08:25 +0000 Subject: 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 --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/number_helper.rb | 9 ++++++++- actionpack/test/template/number_helper_test.rb | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index cff56e65e2..9772ca847e 100644 --- a/actionpack/CHANGELOG +++ b/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] 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 # * :unit - Sets the denomination of the currency (defaults to "$"). # * :separator - Sets the separator between the units (defaults to "."). # * :delimiter - Sets the thousands delimiter (defaults to ","). + # * :format - 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 => "£", :separator => ",", :delimiter => "") # # => £1234567890,50 + # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u") + # # => 1234567890,50 £ 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 diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 29cb9c1701..00e5bd6461 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -25,6 +25,7 @@ class NumberHelperTest < Test::Unit::TestCase assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})) assert_equal("£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})) assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50")) + assert_equal("1,234,567,890.50 Kč", number_to_currency("1234567890.50", {:unit => "Kč", :format => "%n %u"})) assert_equal("$x.", number_to_currency("x")) assert_nil number_to_currency(nil) end -- cgit v1.2.3