number_to_currency
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (1)
- 2.0.3 (1)
- 2.1.0 (5)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (3)
- 3.0.9 (8)
- 3.1.0 (0)
- 3.2.1 (4)
- 3.2.8 (-3)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (8)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (4)
- 6.1.3.1 (1)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (-38)
- 7.1.3.4 (0)
- 7.2.3 (0)
- 8.0.0 (0)
- 8.1.1 (1)
- What's this?
number_to_currency(number, options = {})
public
Delegates to ActiveSupport::NumberHelper#number_to_currency.
Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:
number_to_currency("12x34") # => "$12x34" number_to_currency("12x34", raise: true) # => InvalidNumberError
Brazilian Real (R$ 1.200,95)
helper:
def number_to_currency_br(number) number_to_currency(number, :unit => "R$ ", :separator => ",", :delimiter => ".") end
number_to_euro
in small cells:
12 € --> 12 € def number_to_euro(amount) number_to_currency(amount,:unit=>'€').gsub(' ',nbsp) end
Use this in controllers
Sometimes you’re gonna need this in controllers. Just put this in the controller:
include ActionView::Helpers::NumberHelper
How to change format automatically depending on locale...
… without passing locale option.
In your application_helper.rb (or in other helper) place following code:
def number_to_currency(number, options = {}) options[:locale] ||= I18n.locale super(number, options) end
Then, in your locale files:
en-GB: number: currency: format: format: "%n %u" unit: "USD"
And that is it :)
Bangladeshi Taka (BDT 1,200.95)
Code example
def to_bdt(amount) number_to_currency(amount, :unit => "BDT ", :separator => ".", :delimiter => ",") end
If you happen to face some weird rounding issue...
i.e.
helper.number_to_currency(187) => "190 kr"
check out your… translations! Especially ‘significant’ key… In my case it was
number: currency: format: significant: 'false'
that broke rounding. It should have been
number: currency: format: significant: ! 'false'
And now it works perfectly
helper.number_to_currency(187) => "187 kr"

