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

number_to_currency negative zero fix

This commit is contained in:
Hector Bustillos 2019-12-02 23:18:00 -07:00
parent 76bafb0b2f
commit 92fea7a574
4 changed files with 11 additions and 1 deletions

View file

@ -114,6 +114,8 @@ module ActionView
#
# number_to_currency("123a456", raise: true) # => InvalidNumberError
#
# number_to_currency(-0.456789, precision: 0)
# # => "$0"
# number_to_currency(-1234567890.50, negative_format: "(%u%n)")
# # => ($1,234,567,890.50)
# number_to_currency(1234567890.50, unit: "R$", separator: ",", delimiter: "")

View file

@ -97,6 +97,10 @@ module ActiveSupport
# number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
# number_to_currency('123a456') # => "$123a456"
#
# number_to_currency("123a456", raise: true) # => InvalidNumberError
#
# number_to_currency(-0.456789, precision: 0)
# # => "$0"
# number_to_currency(-1234567890.50, negative_format: '(%u%n)')
# # => "($1,234,567,890.50)"
# number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '')

View file

@ -12,8 +12,11 @@ module ActiveSupport
format = options[:format]
if number.to_f.negative?
format = options[:negative_format]
number = absolute_value(number)
unless options[:precision] == 0 && number.to_f < 0.5
format = options[:negative_format]
end
end
rounded_number = NumberToRoundedConverter.convert(number, options)

View file

@ -77,6 +77,7 @@ module ActiveSupport
assert_equal("1,234,567,890.50 K&#269;", number_helper.number_to_currency("1234567890.50", unit: "K&#269;", format: "%n %u"))
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))
end
end