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

Merge pull request #39350 from jaynetics/fix_rounding_of_custom_formatted_negative_amounts

Fix rounding of custom-formatted negative amounts
This commit is contained in:
Rafael França 2020-08-26 16:08:55 -04:00 committed by GitHub
commit 61716e1fde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View file

@ -9,16 +9,12 @@ module ActiveSupport
def convert
number = self.number.to_s.strip
number_f = number.to_f
format = options[:format]
if number_f.negative?
number = number_f.abs
unless options[:precision] == 0 && number < 0.5
if number.sub!(/^-/, "") &&
(options[:precision] != 0 || number.to_f > 0.5)
format = options[:negative_format]
end
end
rounded_number = NumberToRoundedConverter.convert(number, options)
format.gsub("%n", rounded_number).gsub("%u", options[:unit])

View file

@ -79,6 +79,12 @@ module ActiveSupport
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", number_helper.number_to_currency(-0.456789, precision: 0))
assert_equal("$1,11", number_helper.number_to_currency("1,11"))
assert_equal("$0,11", number_helper.number_to_currency("0,11"))
assert_equal("$,11", number_helper.number_to_currency(",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("-$,11", number_helper.number_to_currency("-,11"))
end
end