Want to receive a weekly email containing
the scoop on our new titles along with the
occasional special offer? Just click the button.
(You can always unsubscribe later by editing your
account information).

Give us an email and a password (you can use the password later to log in and
change your preferences). We'll send you a newsletter roughly once a week.

Pt-C-2

The full description of the number_to_currency helper method is.

number_to_currency(number, options = {})
Formats a number into a currency string. The options hash can be used to customize the for mat of the output. The number can contain a level of precision using the :precision key; default is 2 The currency type can be set using the :unit key; default is ”$” The unit separator can be set using the :separator key; default is ”.” The delimiter can be set using the :delimiter key; default is ”,”.

There is a hidden bug, it took me a while to find it. The variable before always will be true, whether you call the number_to_currency method with :before => true or :before => false or without this parameter. And another bug is that the ternary operator has reversed order (you probably accomodated it to return the unit after the price)

Let me explain. The Ruby Hash class returns by default nil if you try to reference non-existent hash key.
And these are the cases that can we have when calling the number_to_currency method:
First: we ommit the optional hash key :before. The before variable will be true, because the default value returned by non-existent hash key will be nil, which is equivalent to false in ruby boolean expressions.
Second: we call it with :before => true. The before variable will be again true.
Third: we call it with :before => false. Guess what? before is again true.

But anyhow thanks for the clue.

Javi adds:

I’ve added a number_to_currency method to store_helper.rb so it calls ActionView::Helpers::NumberHelper#number_to_currency with different default options. This way the view doesn’t have to be changed.

product_price reflects the so called “item” in the above helper method. To emphasize that, one could use parenthesis:

<%=currency_euro (product.price) -%>

Haeil says:

I think these are not real solutions.
We need more precise internationalization solution, like Locale in java.
Do you know, an Indian write their number like this : 12,34,56,789. How colud we response all of difference…

Skilldrick says:

Internationalization is dealt with in chapter 13. To change the currency to “£”, just put this in config/locales/en.yml:

en:
number:
currency:
format:
unit: "£"

cs says:

Don’t forget to correct the currency sign in Functional test after you changed it from $ to something else… :p

Christian says:

If your are using Trientalis approach (which I like) and are wondering why you are getting an ugly error, it’s because there’s a whitespace too much between the function name and the parenthesis. Took me a while to figure that one out ;)

Young says:

I use the number_to_currency(1234567890.506, :locale => :fr) but the result is not 1 234 567 890,51 € ,can someone tell me Why? I get than from the API

Sergey says:

Read the answers from beginning. You need to change unit: "€" NOT only locale. Locale is for formatting.

Mark says:

I used Skilldrick’s suggestion above to add unit: “£” to config/locales/en.yml. That worked nicely for web page display. I then tried to change the functional test to

assert_select '.price', /£[,\d]+\.\d\d/

However that fails when running rake test:functionals with the error ‘store_controller_test.rb:11: invalid multibyte char (US-ASCII) (SyntaxError)’.
I worked around it with

assert_select '.price', /.\d[,\d]*\.\d\d/

William says:

to fix this, add to the header:
# encoding: utf-8

Mark says:

Thank you for the suggestion William. Where do I add that setting? I’ve looked and I already have config.encoding = “utf-8” in config/application.rb.