Ticket #7892: number_helper_added_unitside_option_with_test.patch
| File number_helper_added_unitside_option_with_test.patch, 2.8 kB (added by kamal, 1 year ago) |
|---|
-
actionpack/test/template/number_helper_test.rb
old new 24 24 assert_equal("$1,234,567,892", number_to_currency(1234567891.50, {:precision => 0})) 25 25 assert_equal("$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})) 26 26 assert_equal("£1234567890,50", number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""})) 27 assert_equal("1234567890.50 kr.", number_to_currency(1234567890.50, {:unit => " kr.", :unitside => "right", :delimiter => ""})) 27 28 assert_equal("$1,234,567,890.50", number_to_currency("1234567890.50")) 28 29 assert_equal("$x.", number_to_currency("x")) 29 30 assert_nil number_to_currency(nil) -
actionpack/lib/action_view/helpers/number_helper.rb
old new 50 50 # in the +options+ hash. 51 51 # 52 52 # ==== Options 53 # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).53 # * <tt>:precision</tt> - Sets the level of precision (defaults to 2). 54 54 # * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$"). 55 # * <tt>:unitside</tt> - Sets the position of the unit (defaults to "left"). 55 56 # * <tt>:separator</tt> - Sets the separator between the units (defaults to "."). 56 57 # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ","). 57 58 # … … 62 63 # 63 64 # number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "") 64 65 # # => £1234567890,50 66 # number_to_currency(15.5, :unit => " kr.", :unitside => "right") 67 # # => 15.5 kr. 65 68 def number_to_currency(number, options = {}) 66 69 options = options.stringify_keys 67 70 precision = options["precision"] || 2 68 71 unit = options["unit"] || "$" 72 unitside = options["unitside"] || "left" 69 73 separator = precision > 0 ? options["separator"] || "." : "" 70 74 delimiter = options["delimiter"] || "," 71 75 72 76 begin 73 77 parts = number_with_precision(number, precision).split('.') 74 unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 78 if unitside == "left" 79 unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s 80 elsif unitside == "right" 81 number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s + unit 82 end 75 83 rescue 76 84 number 77 85 end