Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9052

Show
Ignore:
Timestamp:
03/17/08 22:08:25 (2 months ago)
Author:
david
Message:

Added :format option to NumberHelper#number_to_currency to enable better localization support #11149 [lylo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r9030 r9052  
    11*SVN* 
     2 
     3* Added :format option to NumberHelper#number_to_currency to enable better localization support #11149 [lylo] 
    24 
    35* Fixed that TextHelper#excerpt would include one character too many #11268 [Irfy] 
  • trunk/actionpack/lib/action_view/helpers/number_helper.rb

    r8774 r9052  
    5555      # * <tt>:separator</tt>  - Sets the separator between the units (defaults to "."). 
    5656      # * <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 
    5761      # 
    5862      # ==== Examples 
     
    6367      #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "") 
    6468      #  # => &pound;1234567890,50 
     69      #  number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u") 
     70      #  # => 1234567890,50 &pound; 
    6571      def number_to_currency(number, options = {}) 
    6672        options   = options.stringify_keys 
     
    6975        separator = precision > 0 ? options["separator"] || "." : "" 
    7076        delimiter = options["delimiter"] || "," 
     77        format    = options["format"] || "%u%n" 
    7178 
    7279        begin 
    7380          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) 
    7582        rescue 
    7683          number 
  • trunk/actionpack/test/template/number_helper_test.rb

    r8774 r9052  
    2626    assert_equal("&pound;1234567890,50", number_to_currency(1234567890.50, {:unit => "&pound;", :separator => ",", :delimiter => ""})) 
    2727    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"})) 
    2829    assert_equal("$x.", number_to_currency("x")) 
    2930    assert_nil number_to_currency(nil)