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 4b8d267b4e..c0efababa9 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 @@ -9,15 +9,11 @@ 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 - format = options[:negative_format] - end + if number.sub!(/^-/, "") && + (options[:precision] != 0 || number.to_f > 0.5) + format = options[:negative_format] end rounded_number = NumberToRoundedConverter.convert(number, options) diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index 8b346dd469..579ba73ede 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -79,6 +79,12 @@ module ActiveSupport 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", 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