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.
This commit is contained in:
Prathamesh Sonpatki 2016-01-29 11:46:39 +05:30
parent c942298fda
commit d3f178bb92
3 changed files with 16 additions and 6 deletions

View File

@ -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*

View File

@ -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

View File

@ -74,7 +74,6 @@ module ActiveSupport
assert_equal("1,234,567,890.50 Kč", number_helper.number_to_currency("1234567890.50", {:unit => "Kč", :format => "%n %u"}))
assert_equal("1,234,567,890.50 - Kč", number_helper.number_to_currency("-1234567890.50", {:unit => "Kč", :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