-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix number_to_currency regression in handling "-0.0" #43098
Fix number_to_currency regression in handling "-0.0" #43098
Conversation
080cab2
to
adc3fbb
Compare
activesupport/lib/active_support/number_helper/number_to_currency_converter.rb
Outdated
Show resolved
Hide resolved
adc3fbb
to
356acf2
Compare
356acf2
to
3dbb3e1
Compare
simplifying the method along the way. This regressed in rails#42581 and is related to prior work in rails#39350 and rails#37865.
3dbb3e1
to
81175fb
Compare
end | ||
number_s = NumberToRoundedConverter.convert(number_f, options) | ||
else | ||
number_s = number.to_s.strip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great refactoring!
If the number isn't a valid float ("123.0_badstring") should it always return 0.0
instead of the invalid value?
Otherwise this might be another regression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@p8 Thanks for asking, the previous behavior is actually to just slap a currency symbol on the front of invalid strings (with some light handling of "-"), so that "alternate formats" like -1,23
get formatted as -$1,23
:
ActiveSupport::NumberHelper.number_to_currency("-123.0_badstring")
# => "-$123.0_badstring"
This behavior is preserved in the refactoring! And the test coverage is pretty good for this helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe more directly to your point: the behavior of NumberToRoundedConverter is to check (again) if the number is valid and if not to just return the input string. Avoiding that expensive no-op is part of why invalid strings are so much faster with this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flavorjones Thanks for the clarification! That makes sense. 😄
This fixes a regression introduced in #42581, and is related to prior work in #39350 and #37865.
Summary
In a follow-up comment on #42581 it was noted that
number_to_currency(-0.0)
returned"-$0.00"
which was a change from the method's previous behavior which returned"$0.00"
.That regression came about because of the logic in
number_to_currency
which attempted to detect a "parse failure" by checking for== 0.0
(see #39350 for context on gracefully dealing with "alternate input formats").This PR simplifies the logic of the method, explicitly handling the "invalid string" case and improving performance for both the case where input is a
Float
and where it's an invalid string.Benchmarks
I benchmarked a Float, a valid string, and an invalid string against
main
and this PR:The results: