diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-10-09 00:59:19 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-10-09 00:59:19 +0000 |
commit | a49e7d5c0c5dc9f48d7c0b5a620d1528d998accc (patch) | |
tree | 6940639fad9dba18af32612b03c01590e6747410 /actionpack | |
parent | f9650a23f060c0d8678b2bda45e502f0bca487cc (diff) | |
download | rails-a49e7d5c0c5dc9f48d7c0b5a620d1528d998accc.tar.gz rails-a49e7d5c0c5dc9f48d7c0b5a620d1528d998accc.tar.bz2 rails-a49e7d5c0c5dc9f48d7c0b5a620d1528d998accc.zip |
Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits (closes #6231) [phallstrom]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5249 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/number_helper.rb | 12 | ||||
-rw-r--r-- | actionpack/test/template/number_helper_test.rb | 9 |
3 files changed, 20 insertions, 3 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 224874188d..007736f2bb 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits #6231 [phallstrom] + * Fixed that FormHelper#radio_button didn't respect an :id being passed in #6266 [evansj] * Added an html_options hash parameter to javascript_tag() and update_page_tag() helpers #6311 [tzaharia]. Example: diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index cb312320ef..1c9912f877 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -71,10 +71,16 @@ module ActionView # Formats a +number+ with a +delimiter+. # Example: # number_with_delimiter(12345678) => 12,345,678 - def number_with_delimiter(number, delimiter=",") - number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") + def number_with_delimiter(number, delimiter=",", separator=".") + begin + parts = number.to_s.split(separator) + parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") + parts.join separator + rescue + number + end end - + # Returns a formatted-for-humans file size. # # Examples: diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index fd418d1e06..53e693382a 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -31,6 +31,15 @@ class NumberHelperTest < Test::Unit::TestCase def test_number_with_delimiter assert_equal("12,345,678", number_with_delimiter(12345678)) + assert_equal(nil, number_with_delimiter(nil)) + assert_equal("0", number_with_delimiter(0)) + assert_equal("123", number_with_delimiter(123)) + assert_equal("123,456", number_with_delimiter(123456)) + assert_equal("123,456.78", number_with_delimiter(123456.78)) + assert_equal("123,456.789", number_with_delimiter(123456.789)) + assert_equal("123,456.78901", number_with_delimiter(123456.78901)) + assert_equal("123,456,789.78901", number_with_delimiter(123456789.78901)) + assert_equal("0.78901", number_with_delimiter(0.78901)) end def test_number_to_human_size |