Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

number_to_currency doesn't format negative numbers correctly

#5894

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.

Reported by Don Wilson · October 30th, 2010 @ 08:18 AM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Don Wilson
    Don Wilson

    Whoops, I should have put [PATCH] in the subject and I didn't.

    October 30th, 2010 @ 08:26 AM

  2. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • 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 :)

    October 31st, 2010 @ 12:35 PM

  3. Don Wilson
    Don Wilson

    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.

    October 31st, 2010 @ 03:41 PM

  4. Andrew White
    Andrew White
    • 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.

    November 1st, 2010 @ 06:34 AM

  5. Rodrigo Rosenfeld Rosas
    Rodrigo Rosenfeld Rosas

    I have a client in Brazil that complaint exactly about the negative format used by Rails, so +1!

    November 1st, 2010 @ 12:55 PM

  6. Don Wilson
    Don Wilson

    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?

    November 1st, 2010 @ 03:10 PM

  7. Andrew White
    Andrew White

    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.

    November 1st, 2010 @ 04:00 PM

  8. Don Wilson
    Don Wilson

    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!

    November 2nd, 2010 @ 04:16 AM

  9. Andrew White
    Andrew White

    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.

    November 2nd, 2010 @ 11:19 AM

  10. Don Wilson
  11. Repository
  12. Repository
  13. Jeff Kreeftmeijer
  14. nhatmy
  15. jackal
    jackal

    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

    April 11th, 2011 @ 09:54 PM

  16. asil
    asil

    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

    April 24th, 2011 @ 09:58 PM

  17. frontdata
    frontdata

    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

    April 26th, 2011 @ 05:56 PM

  18. SEO Company India
    SEO Company India

    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!

    May 7th, 2011 @ 11:30 AM