| 1 |
Index: test/template/number_helper_test.rb |
|---|
| 2 |
=================================================================== |
|---|
| 3 |
--- test/template/number_helper_test.rb (revision 8894) |
|---|
| 4 |
+++ test/template/number_helper_test.rb (working copy) |
|---|
| 5 |
@@ -25,6 +25,7 @@ |
|---|
| 6 |
assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})) |
|---|
| 7 |
assert_equal("£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})) |
|---|
| 8 |
assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50")) |
|---|
| 9 |
+ assert_equal("1,234,567,890.50 Kč", number_to_currency("1234567890.50", {:unit => "Kč", :format => "%n %u"})) |
|---|
| 10 |
assert_equal("$x.", number_to_currency("x")) |
|---|
| 11 |
assert_nil number_to_currency(nil) |
|---|
| 12 |
end |
|---|
| 13 |
Index: lib/action_view/helpers/number_helper.rb |
|---|
| 14 |
=================================================================== |
|---|
| 15 |
--- lib/action_view/helpers/number_helper.rb (revision 8894) |
|---|
| 16 |
+++ lib/action_view/helpers/number_helper.rb (working copy) |
|---|
| 17 |
@@ -54,7 +54,11 @@ |
|---|
| 18 |
# * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$"). |
|---|
| 19 |
# * <tt>:separator</tt> - Sets the separator between the units (defaults to "."). |
|---|
| 20 |
# * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ","). |
|---|
| 21 |
+ # * <tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are: |
|---|
| 22 |
# |
|---|
| 23 |
+ # %u The currency unit |
|---|
| 24 |
+ # %n The number |
|---|
| 25 |
+ # |
|---|
| 26 |
# ==== Examples |
|---|
| 27 |
# number_to_currency(1234567890.50) # => $1,234,567,890.50 |
|---|
| 28 |
# number_to_currency(1234567890.506) # => $1,234,567,890.51 |
|---|
| 29 |
@@ -62,16 +66,19 @@ |
|---|
| 30 |
# |
|---|
| 31 |
# number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "") |
|---|
| 32 |
# # => £1234567890,50 |
|---|
| 33 |
+ # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u") |
|---|
| 34 |
+ # # => 1234567890,50 £ |
|---|
| 35 |
def number_to_currency(number, options = {}) |
|---|
| 36 |
options = options.stringify_keys |
|---|
| 37 |
precision = options["precision"] || 2 |
|---|
| 38 |
unit = options["unit"] || "$" |
|---|
| 39 |
separator = precision > 0 ? options["separator"] || "." : "" |
|---|
| 40 |
delimiter = options["delimiter"] || "," |
|---|
| 41 |
+ format = options["format"] || "%u%n" |
|---|
| 42 |
|
|---|
| 43 |
begin |
|---|
| 44 |
parts = number_with_precision(number, precision).split('.') |
|---|
| 45 |
- unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s |
|---|
| 46 |
+ format.gsub(/%n/, number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s).gsub(/%u/, unit) |
|---|
| 47 |
rescue |
|---|
| 48 |
number |
|---|
| 49 |
end |
|---|