This project is archived and is in readonly mode.
number_to_currency doesn't format negative numbers correctly
Reported by Don Wilson | October 30th, 2010 @ 08:18 AM | in 3.0.2
The Problem
Right now, if you call number_to_currency(-1.23), it will return "$-1.23". This is not correct in most cases.
Desired Output
I have done a lot of research on this and I have attached a patch that I think is a good solution. It is not easy to find a list of currency formats that also includes the negative format, but here are a few different ways that it is formatted:
UK -£127.54
France -127,54 F
Denmark kr-127,54
Netherlands € 127,54-
US ($127.54) or -$127.54
Obviously, the most different pattern is the US pattern with parenthesis. I also found some people suggesting that others use {} or <> around negative numbers, but I wasn't able to find good sources on that.
How Others Have Solved This
In brainstorming a solution, I found some examples of how others have tackled the problem. In JSTL, you do write this:
<fmt:formatNumber type="currency"
pattern="$#,##0.00;-$#,##0.00" value="-127.54" />
The semicolon in the pattern separates the positive from the negative pattern.
The .NET Framework has a CurrencyNegativePattern property that has 16 statically stored options for the negative pattern (View on MSDN).
I also noticed that there was a patch submitted a few years ago in Trac for this, but was never incorporated (See Trac Ticket). Look at the ticket to see the discussion there.
My Suggestion
The solution that I propose is to add a :negative_pattern option with the default of "-%u%n". Having a :negative_pattern option provides a lot of flexibility in the pattern including having () or {} or <> around the pattern (if all those patterns really exist). It also makes it easy to specify the position of the negative sign easily or to add a negative sign and a space ("- $127.54") which is a preference that I saw at times researching this.
Comments and changes to this ticket
-
Don Wilson October 30th, 2010 @ 08:26 AM
Whoops, I should have put [PATCH] in the subject and I didn't.
-
Prem Sichanugrist (sikachu) October 31st, 2010 @ 12:35 PM
- Importance changed from to Low
+1 for this patch. Test case looks good.
However, I somehow think it would be nicer if I supply
%n %u
as the format and it would automatically set-%n %u
as the negative format. Do you think that would be possible?Thank you.
P.S. the
[PATCH]
convention is not required anymore, so no problem :) -
Don Wilson October 31st, 2010 @ 03:41 PM
Good idea. I have made that change. The negative format is now "-" + :format by default if a format is specified. Of course, if they specify :negative_format, that format is used.
For example:
number_to_currency(-1.23) => "-$1.23"
number_to_currency(-1.23, {:format => "%n %u"}) => "-1.23 $"
number_to_currency(-1.23, {:format => "%n %u", :negative_format => "(%n %u)"}) => "(1.23 $)"The new patch is attached.
-
Andrew White November 1st, 2010 @ 06:34 AM
- State changed from new to open
- Milestone set to 3.0.2
- Assigned user set to Andrew White
Don, since the format is looked up in the locale we should do the same for negative_format. That way you only have to override it in one place. Also can you squash the patch into one commit.
-
Rodrigo Rosenfeld Rosas November 1st, 2010 @ 12:55 PM
I have a client in Brazil that complaint exactly about the negative format used by Rails, so +1!
-
Don Wilson November 1st, 2010 @ 03:10 PM
Andrew, I'll definitely squash the patch into one commit. I realized I had done that after I uploaded it.
I'm not sure what you mean by your other suggestion, though. I agree that the negative_format should also be looked up in the locale. However, if a user specifically passes in a format, I think the negative_format should still be overridden to be the "-" + the format specified regardless of the locale.
Can you explain what you mean?
-
Andrew White November 1st, 2010 @ 04:00 PM
Absolutely the passed option should override the locale format - I just meant that you could specify it in your locale file to override it throughout the application. Could you also do one patch for master and another for 3-0-stable please.
-
Don Wilson November 2nd, 2010 @ 04:16 AM
OK, thanks for clarifying that for me. It makes sense now.
I have squashed the patch into one commit and I attached a patch for master and another for 3-0-stable!
-
Andrew White November 2nd, 2010 @ 11:19 AM
Don, can you also add some tests to number_helper_i18n_test.rb to test the locale lookup please, so that we don't get any regressions - similar to the tests already there but with negative numbers.
-
Repository November 2nd, 2010 @ 11:08 PM
- State changed from open to resolved
(from [b91371763e1db1c7b7603777e5b18a4da4e09539]) Added :negative_format option to number_to_currency function [#5894 state:resolved]
Signed-off-by: Andrew White andyw@pixeltrix.co.uk
http://github.com/rails/rails/commit/b91371763e1db1c7b7603777e5b18a... -
Repository November 2nd, 2010 @ 11:08 PM
(from [c4d977fbbc31b0a6c7557846f9fe01e688a6f845]) Added :negative_format option to number_to_currency function [#5894 state:resolved]
Signed-off-by: Andrew White andyw@pixeltrix.co.uk
http://github.com/rails/rails/commit/c4d977fbbc31b0a6c7557846f9fe01... -
jackal April 11th, 2011 @ 09:54 PM
very nice pictures is also very lively and attractive you look .every one who wanted to congratulate you You look super. Ankara nakliyat A wonderful picture,nakliyat ankara and always such a special and very attractive as well stay beautiful ok Ankara evden eve nakliyat Ankara nakliye
-
asil April 24th, 2011 @ 09:58 PM
I Agree "However, if a user specifically passes in a format, I think the negative_format should still be overridden to be the "-" + the format specified regardless of the locale."
by antalya hotels -
frontdata April 26th, 2011 @ 05:56 PM
I disagree that Krugman is any more reliable a source than Romney on the stimulus. They are equally hostage to their respective partisan agendas, and have an equal disrespect for the actual situation, not that either of them bother to investigate it. hosting
-
SEO Company India May 7th, 2011 @ 11:30 AM
It's pretty interesting and informative topic. This is exactly what I was looking for. I shared this post to my Facebook account. Keep up the good work!
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 6653 [PATCH] number_to_currency should take configured format for default negative format By resolved ticket #5894 number_to_currency compose defau...
- 5894 number_to_currency doesn't format negative numbers correctly (from [b91371763e1db1c7b7603777e5b18a4da4e09539]) Added :...
- 5894 number_to_currency doesn't format negative numbers correctly (from [c4d977fbbc31b0a6c7557846f9fe01e688a6f845]) Added :...