Override number_to_currency in Rails

Ruby on Rails Add comments

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.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

One Response to “Override number_to_currency in Rails”

  1. Yoann Jean Says:

    Isn’t it “s = number_to_currency(number, defaults.merge(options))” ?

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in