Forum: Ruby on Rails number_to_currency UK pounds

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Rob N. (Guest)
on 2006-02-28 13:33
I work in the UK and therefore most of my currency figures will be in UK
pounds. I realise that to format a number to a currency format I can

number_to_currency(amt, :unit => "£")

but it would be more convenient to set £ as the default currency symbol.
What is the best way to do that?

Should I edit line 39 of this file:


so that options.delete("unit") { "$" } becomes options.delete("unit") {
"£" }

or is there a better way?

Can I set £ as the default at the web application level? An addition to
the \app\helpers within the website files?

I am a newbie to Ruby and Rail. (working my way through "Agile Web
Development with Rails"), so more interested in best practice that quick
Harvey B. (Guest)
on 2006-02-28 13:40
(Received via mailing list)
I've created a helper method like this

  # Displays a textual representation of +number+ in british pound
format (i.e.:
  # "£1,359.56").
  def number_to_currency_gbp (number)
    number_to_currency(number, { :unit => "£"})

So just call number_to_currency_gbp

Hope that helps
Rob N. (Guest)
on 2006-02-28 14:06
Thank you Harvey. I'd come up with something similar:

def fmt_pounds(amt)
 number_to_currency(amt, :unit => "£")

Perhaps I should be happy with that, but it seems neater to me to use
the built in helper, and alter the default.

OK - I've copied the number_to_currency code from number_helper.rb to
application_helper.rd in my web application. I then edited it so the
code looks like this:

def number_to_currency(number, options = {})
	options = options.stringify_keys
	precision, unit, separator, delimiter = options.delete("precision") { 2
}, options.delete("unit") { "£" }, options.delete("separator") { "." },
options.delete("delimiter") { "," }
	separator = "" unless precision > 0
		parts = number_with_precision(number, precision).split('.')
		unit + number_with_delimiter(parts[0], delimiter) + separator +

Now within the application number_to_currency(10.3) produces £10.30.
Which is what I want. Is this good practice? Have I broken anything?
Harvey B. (Guest)
on 2006-02-28 17:17
(Received via mailing list)

The way I suggested seems to be the cleanest solution to me.  I
personally don't like the idea of copying Rails code into your own
application.  I would have thought that it may cause your maintanability
problems in the future.  I'm a newbie to Rails myself so there maybe a
neater way to alter the default but I don't know what it is.  Let me
know if you find a better solution.

Rob N. (Guest)
on 2006-02-28 17:47
Harvey B. wrote:
> I personally don't like the idea of copying Rails code into your own
> application.

I agree. I'm sure I can adjust the way the function behaves without
rewriting all the code. In a couple of places "The Programming Ruby
Guide" ( discusses changing
or adding functionality for inbuilt methods and classes. However, with
this example I'm not sure how to do that.

I'm sure there is a neater way of doing this.

Thanks again for your help
This topic is locked and can not be replied to.