Skip to content

Commit

Permalink
fix number_to_percentage with Float::NAN, Float::INFINITY.
Browse files Browse the repository at this point in the history
Closes #14405.

This is a follow-up to 9e997e9 to restore
the documented behavior.
  • Loading branch information
senny committed Mar 17, 2014
1 parent a0c1c18 commit 378c8d2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,10 @@
* `number_to_percentage` does not crash with `Float::NAN` or `Float::INFINITY`
as input.

Fixes #14405.

*Yves Senn*

* Add `include_hidden` option to `collection_check_boxes` helper.

*Vasiliy Ermolovich*
Expand Down
3 changes: 3 additions & 0 deletions actionview/test/template/number_helper_test.rb
Expand Up @@ -32,6 +32,9 @@ def test_number_to_percentage
assert_equal "100%", number_to_percentage(100, precision: 0)
assert_equal "123.4%", number_to_percentage(123.400, precision: 3, strip_insignificant_zeros: true)
assert_equal "1.000,000%", number_to_percentage(1000, delimiter: ".", separator: ",")
assert_equal "98a%", number_to_percentage("98a")
assert_equal "NaN%", number_to_percentage(Float::NAN)
assert_equal "Inf%", number_to_percentage(Float::INFINITY)
end

def test_number_with_delimiter
Expand Down
Expand Up @@ -32,8 +32,7 @@ def convert
end

formatted_string =
case rounded_number
when BigDecimal
if BigDecimal === rounded_number && rounded_number.finite?
s = rounded_number.to_s('F') + '0'*precision
a, b = s.split('.', 2)
a + '.' + b[0, precision]
Expand Down
3 changes: 3 additions & 0 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -79,6 +79,9 @@ def test_number_to_percentage
assert_equal("123.4%", number_helper.number_to_percentage(123.400, :precision => 3, :strip_insignificant_zeros => true))
assert_equal("1.000,000%", number_helper.number_to_percentage(1000, :delimiter => '.', :separator => ','))
assert_equal("1000.000 %", number_helper.number_to_percentage(1000, :format => "%n %"))
assert_equal("98a%", number_helper.number_to_percentage("98a"))
assert_equal("NaN%", number_helper.number_to_percentage(Float::NAN))
assert_equal("Inf%", number_helper.number_to_percentage(Float::INFINITY))
end
end

Expand Down

1 comment on commit 378c8d2

@wogg
Copy link

@wogg wogg commented on 378c8d2 Mar 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug still exists if "precision: any_int_here" is passed in along with Float::NaN.

Please sign in to comment.