Override number_to_currency in Rails

This article explains the approach but I had problems getting the implementation to work (can't convert BigDecimal into String errors). So here’s my solutions:

Create a file in #{RAILS_ROOT}/config/initializers, which you can name anything you want, for example my_currency_helpers.rb

Paste this code:

module ActionView module Helpers module NumberHelper def number_to_currency_with_euro(number, options = {}) defaults = { :unit => ‘€’, #obvious :precision => 2, #digits after separator :separator => ',', :delimiter => '.', #thousands delimiter :format => "%n %u" } #put a space between number (%n) and unit (%u) and unit is after number s = number_to_currency_without_euro(number, defaults.merge(options)) end alias_method_chain :number_to_currency, :euro end end end

The above is the formatting for the Euro in Germany, you can easily adjust this code to work with other currencies. Just copy and paste the same code in the same file, rename alias and function and adjust the options. Hope it helps.

Yoann Jean: Isn't it "s = number_to_currency(number, defaults.merge(options))" ?

Posted: 12 February 2009

comments powered by Disqus