Permalink
Browse files

fix `number_to_percentage` with `Float::NAN`, `Float::INFINITY`.

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 378c8d2c996558aa4108280d5f0db8daf040d0fc
View
@@ -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*
@@ -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
@@ -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]
@@ -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

1 comment on commit 378c8d2

@wogg

This comment has been minimized.

Show comment Hide comment
@wogg

wogg Mar 5, 2015

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

wogg commented on 378c8d2 Mar 5, 2015

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

Please sign in to comment.