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

Changeset 9086

Show
Ignore:
Timestamp:
03/24/08 21:12:55 (3 months ago)
Author:
david
Message:

Fixed NumberHelper#number_with_precision to properly round in a way that works equally on Mac, Windows, Linux (closes #11409, #8275, #10090, #8027) [zhangyuanyi]

Files:

Legend:

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

    r9083 r9086  
    11*SVN* 
    22 
    3 # Allow the #simple_format text_helper to take an html_options hash for each paragraph.  #2448 [Francois Beausoleil, thechrisoshow] 
     3* Fixed NumberHelper#number_with_precision to properly round in a way that works equally on Mac, Windows, Linux (closes #11409, #8275, #10090, #8027) [zhangyuanyi] 
     4 
     5* Allow the #simple_format text_helper to take an html_options hash for each paragraph.  #2448 [Francois Beausoleil, thechrisoshow] 
    46 
    57* Fix regression from filter refactoring where re-adding a skipped filter resulted in it being called twice.  [rick] 
  • trunk/actionpack/lib/action_view/helpers/number_helper.rb

    r9052 r9086  
    145145      # ==== Examples 
    146146      #  number_with_precision(111.2345)     # => 111.235 
    147       #  number_with_precision(111.2345, 2)  # => 111.24 
     147      #  number_with_precision(111.2345, 2)  # => 111.23 
    148148      #  number_with_precision(13, 5)        # => 13.00000 
    149149      #  number_with_precision(389.32314, 0) # => 389 
    150150      def number_with_precision(number, precision=3) 
    151         "%01.#{precision}f" % number 
     151        "%01.#{precision}f" % ((Float(number) * (10 ** precision)).round.to_f / 10 ** precision) 
    152152      rescue 
    153153        number 
  • trunk/actionpack/test/template/number_helper_test.rb

    r9052 r9086  
    5757  def test_number_with_precision 
    5858    assert_equal("111.235", number_with_precision(111.2346)) 
     59    assert_equal("31.83", number_with_precision(31.825, 2))     
    5960    assert_equal("111.23", number_with_precision(111.2346, 2)) 
    6061    assert_equal("111.00", number_with_precision(111, 2)) 
    6162    assert_equal("111.235", number_with_precision("111.2346")) 
     63    assert_equal("31.83", number_with_precision("31.825", 2)) 
    6264    assert_equal("112", number_with_precision(111.50, 0)) 
    6365    assert_equal("1234567892", number_with_precision(1234567891.50, 0))