Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix handling of negative zero in number_to_currency #6512

Merged
merged 1 commit into from

3 participants

Jared Beck Arun Agrawal José Valim
Jared Beck

Without this patch, number_to_currency() treats -0.0 as if it were a positive number and fails to apply the :negative_format option.

I tried to write this to support multiple rubies. In modern rubies, it will invoke phase() but looking at the docs for older rubies I couldn't find a satisfactory alternative, and had to settle for slicing the first char off of to_s(). Please tell me there's a better way. :-)

Arun Agrawal arunagw closed this
Arun Agrawal arunagw reopened this
Arun Agrawal
Collaborator

Not sure why. But this PR cannot merge automatically. Can you please do a rebase again ?

Arun Agrawal
Collaborator

Sorry I press on wrong button :-(

Jared Beck

Hmmm .. I think @josevalim just moved everything in that file .. doh. 135f620

José, should I re-write this for ActiveSupport? (activesupport/lib/active_support/number_helper.rb)

José Valim
Owner

@jaredbeck please do rewrite! A small note though: Rails master only supports Ruby 1.9.3+, so you can just go ahead and use phase. :)

Jared Beck
Rails master only supports Ruby 1.9.3+, so you can just go ahead and use phase. :)

oh thank goodness, that's much less messy then.

re-written for activesupport, thanks.

José Valim josevalim merged commit 5acb10d into from
José Valim
Owner

Merged! Thanks for the contribution and teaching me about phase :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 28, 2012
  1. Jared Beck
This page is out of date. Refresh to see the latest.
2  activesupport/lib/active_support/number_helper.rb
View
@@ -111,7 +111,7 @@ def number_to_currency(number, options = {})
unit = options.delete(:unit)
format = options.delete(:format)
- if number.to_f < 0
+ if number.to_f.phase != 0
format = options.delete(:negative_format)
number = number.respond_to?("abs") ? number.abs : number.sub(/^-/, '')
end
2  activesupport/test/number_helper_test.rb
View
@@ -64,6 +64,8 @@ def test_number_to_currency
assert_equal("$1,234,567,890.50", number_helper.number_to_currency("1234567890.50"))
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.00)", number_helper.number_to_currency(-0.0, {:unit => "", :negative_format => "(%n)"}))
end
end
Something went wrong with that request. Please try again.