1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix number_to_currency regression in handling "-0.0"

simplifying the method along the way.

This regressed in #42581 and is related to prior work in #39350 and #37865.
This commit is contained in:
Mike Dalessio 2021-08-25 08:16:27 -04:00
parent 326d88660f
commit 81175fb98a
No known key found for this signature in database
GPG key ID: 59D3039C71577DD7
2 changed files with 12 additions and 12 deletions

View file

@ -8,23 +8,21 @@ module ActiveSupport
self.namespace = :currency self.namespace = :currency
def convert def convert
number = self.number.to_s.strip
format = options[:format] format = options[:format]
if number.sub!(/^-/, "") number_f = valid_float?
number_f = number.to_f.abs if number_f
if number_f == 0.0 if number_f.negative?
# likely an alternate input format that failed to parse. number_f = number_f.abs
# see https://github.com/rails/rails/pull/39350 format = options[:negative_format] if (number_f * 10**options[:precision]) >= 0.5
format = options[:negative_format]
else
number_f *= 10**options[:precision]
format = options[:negative_format] if number_f >= 0.5
end end
number_s = NumberToRoundedConverter.convert(number_f, options)
else
number_s = number.to_s.strip
format = options[:negative_format] if number_s.sub!(/^-/, "")
end end
rounded_number = NumberToRoundedConverter.convert(number, options) format.gsub("%n", number_s).gsub("%u", options[:unit])
format.gsub("%n", rounded_number).gsub("%u", options[:unit])
end end
private private

View file

@ -88,6 +88,8 @@ module ActiveSupport
assert_equal("-$1,11", number_helper.number_to_currency("-1,11")) assert_equal("-$1,11", number_helper.number_to_currency("-1,11"))
assert_equal("-$0,11", number_helper.number_to_currency("-0,11")) assert_equal("-$0,11", number_helper.number_to_currency("-0,11"))
assert_equal("-$,11", number_helper.number_to_currency("-,11")) assert_equal("-$,11", number_helper.number_to_currency("-,11"))
assert_equal("$0.00", number_helper.number_to_currency(-0.0))
assert_equal("$0.00", number_helper.number_to_currency("-0.0"))
end end
end end