This project is archived and is in readonly mode.

#5894 ✓resolved
Don Wilson

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

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>

Referenced by

Pages