aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorPrathamesh Sonpatki <csonpatki@gmail.com>2016-01-29 11:46:39 +0530
committerPrathamesh Sonpatki <csonpatki@gmail.com>2016-01-29 12:21:26 +0530
commitd3f178bb92473b4d7bb400be56c983203b1662e9 (patch)
treeede637d5ca4dfbb8724580030c941c3bb1d1c7cd /activesupport
parentc942298fda223672b74b953b9ce369674033727b (diff)
downloadrails-d3f178bb92473b4d7bb400be56c983203b1662e9.tar.gz
rails-d3f178bb92473b4d7bb400be56c983203b1662e9.tar.bz2
rails-d3f178bb92473b4d7bb400be56c983203b1662e9.zip
Change number_to_currency behavior for checking negativity
- Instead of using `to_f.phase`, just use `to_f.negative`?. - This change works same for all cases except when number is "-0.0". -0.0.to_f.negative? => false -0.0.to_f.phase? => pi - So -0.0 will be treated as positive from now onwards. - So this change reverts changes from https://github.com/rails/rails/pull/6512. - But it should be acceptable as we could not find any currency which supports negative zeros.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md15
-rw-r--r--activesupport/lib/active_support/number_helper/number_to_currency_converter.rb6
-rw-r--r--activesupport/test/number_helper_test.rb1
3 files changed, 16 insertions, 6 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 21c79949ca..7701e0a7a2 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,18 @@
+* Change number_to_currency behavior for checking negativity.
+
+ Used `to_f.negative` instead of using `to_f.phase` for checking negativity
+ of a number in number_to_currency helper.
+ This change works same for all cases except when number is "-0.0".
+
+ -0.0.to_f.negative? => false
+ -0.0.to_f.phase? => 3.14
+
+ This change reverts changes from https://github.com/rails/rails/pull/6512.
+ But it should be acceptable as we could not find any currency which
+ supports negative zeros.
+
+ *Prathamesh Sonpatki*, *Rafael Mendonça França*
+
* Match `HashWithIndifferentAccess#default`'s behaviour with `Hash#default`.
*David Cornu*
diff --git a/activesupport/lib/active_support/number_helper/number_to_currency_converter.rb b/activesupport/lib/active_support/number_helper/number_to_currency_converter.rb
index 7986eb50f0..fe8e871fed 100644
--- a/activesupport/lib/active_support/number_helper/number_to_currency_converter.rb
+++ b/activesupport/lib/active_support/number_helper/number_to_currency_converter.rb
@@ -7,7 +7,7 @@ module ActiveSupport
number = self.number.to_s.strip
format = options[:format]
- if is_negative?(number)
+ if number.to_f.negative?
format = options[:negative_format]
number = absolute_value(number)
end
@@ -18,10 +18,6 @@ module ActiveSupport
private
- def is_negative?(number)
- number.to_f.phase != 0
- end
-
def absolute_value(number)
number.respond_to?(:abs) ? number.abs : number.sub(/\A-/, '')
end
diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb
index b3464462c8..6696111476 100644
--- a/activesupport/test/number_helper_test.rb
+++ b/activesupport/test/number_helper_test.rb
@@ -74,7 +74,6 @@ module ActiveSupport
assert_equal("1,234,567,890.50 K&#269;", number_helper.number_to_currency("1234567890.50", {:unit => "K&#269;", :format => "%n %u"}))
assert_equal("1,234,567,890.50 - K&#269;", number_helper.number_to_currency("-1234567890.50", {:unit => "K&#269;", :format => "%n %u", :negative_format => "%n - %u"}))
assert_equal("0.00", number_helper.number_to_currency(+0.0, {:unit => "", :negative_format => "(%n)"}))
- assert_equal("(0.00)", number_helper.number_to_currency(-0.0, {:unit => "", :negative_format => "(%n)"}))
end
end