diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-03-06 09:04:51 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-03-06 09:04:51 +0100 |
commit | 081a3963ea2aab617a92874d7ef59ce98eee6a64 (patch) | |
tree | 56b4d4d672726a66ef612c34e857fb8eee45fdd3 /activesupport | |
parent | 1c1bced7b17c9b0d2866f43bd2b35a1be16dbcf0 (diff) | |
download | rails-081a3963ea2aab617a92874d7ef59ce98eee6a64.tar.gz rails-081a3963ea2aab617a92874d7ef59ce98eee6a64.tar.bz2 rails-081a3963ea2aab617a92874d7ef59ce98eee6a64.zip |
`number_to_percentage` and `precision: 0` work with `NAN` and `INFINITY`.
Closes #19227.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb | 2 | ||||
-rw-r--r-- | activesupport/test/number_helper_test.rb | 4 |
3 files changed, 12 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f5fa70494a..1d21a18c16 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* `number_to_percentage` does not crash with `Float::NAN` or `Float::INFINITY` + as input when `precision: 0` is used. + + Fixes #19227. + + *Yves Senn* + * Take DST into account when locating TimeZone from Numeric. When given a specific offset, use the first result found where the diff --git a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb index df316a08e6..981c562551 100644 --- a/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb +++ b/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb @@ -23,7 +23,7 @@ module ActiveSupport precision = 0 if precision < 0 # don't let it be negative else rounded_number = number.round(precision) - rounded_number = rounded_number.to_i if precision == 0 + rounded_number = rounded_number.to_i if precision == 0 && rounded_number.finite? rounded_number = rounded_number.abs if rounded_number.zero? # prevent showing negative zeros end diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index 23996ef381..83efbffdfb 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -83,6 +83,10 @@ module ActiveSupport 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)) + assert_equal("NaN%", number_helper.number_to_percentage(Float::NAN, precision: 0)) + assert_equal("Inf%", number_helper.number_to_percentage(Float::INFINITY, precision: 0)) + assert_equal("NaN%", number_helper.number_to_percentage(Float::NAN, precision: 1)) + assert_equal("Inf%", number_helper.number_to_percentage(Float::INFINITY, precision: 1)) assert_equal("1000%", number_helper.number_to_percentage(1000, precision: nil)) assert_equal("1000%", number_helper.number_to_percentage(1000, precision: nil)) assert_equal("1000.1%", number_helper.number_to_percentage(1000.1, precision: nil)) |