From 8489a5e1148259fd0722fc9302da4b92379f3ae2 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Wed, 23 Jun 2021 12:47:42 -0400 Subject: [PATCH] Fix number_to_currency to use negative format when rounding 0.5 Previously this function would fail to apply the negative format correctly if the value being rounded was 0.5 exactly (modulo precision). This fixes the logic to use `>= 0.5` instead of `> 0.5` --- .../number_helper/number_to_currency_converter.rb | 2 +- activesupport/test/number_helper_test.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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 d1f0a4cad6..4d0f9bd437 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 @@ -19,7 +19,7 @@ module ActiveSupport format = options[:negative_format] else number_f *= 10**options[:precision] - format = options[:negative_format] if number_f > 0.5 + format = options[:negative_format] if number_f >= 0.5 end end diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index 78d6c23f83..dcda16eb98 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -81,6 +81,7 @@ module ActiveSupport assert_equal("$0", number_helper.number_to_currency(-0.456789, precision: 0)) assert_equal("$0.0", number_helper.number_to_currency(-0.0456789, precision: 1)) assert_equal("$0.00", number_helper.number_to_currency(-0.00456789, precision: 2)) + assert_equal("-$1", number_helper.number_to_currency(-0.5, 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"))