number_to_currency
![Very extensive documentation Importance_5](https://d2vfyqvduarcvs.cloudfront.net/images/importance_5.png?1349367920)
number_to_currency(number, options = {})
public
Formats a number into a currency string. The options hash can be used to customize the format 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 "," Examples:
number_to_currency(1234567890.50) => $1,234,567,890.50 number_to_currency(1234567890.506) => $1,234,567,890.51 number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""}) => £1234567890,50
![Default_avatar_30](https://www.gravatar.com/avatar/3eca00a0d3f5725d81723b69621287bd?default=http://apidock.com/images/default_avatar_30.png&size=30)
Brazilian Real (R$ 1.200,95)
helper:
def number_to_currency_br(number) number_to_currency(number, :unit => "R$ ", :separator => ",", :delimiter => ".") end
![Default_avatar_30](https://www.gravatar.com/avatar/59436ecd4fe6ad7c34f67654d839f05f?default=http://apidock.com/images/default_avatar_30.png&size=30)
number_to_euro
in small cells:
12 € --> 12 € def number_to_euro(amount) number_to_currency(amount,:unit=>'€').gsub(' ',nbsp) end
![Default_avatar_30](https://www.gravatar.com/avatar/444232854675243c39401d579d3742bd?default=http://apidock.com/images/default_avatar_30.png&size=30)
Use this in controllers
Sometimes you’re gonna need this in controllers. Just put this in the controller:
include ActionView::Helpers::NumberHelper
![Default_avatar_30](https://www.gravatar.com/avatar/16b8d429e0123bc50ba8ad27cdaa6ff4?default=http://apidock.com/images/default_avatar_30.png&size=30)
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 :)
![Default_avatar_30](https://www.gravatar.com/avatar/357f60eb961f6c38257245a175ee3a43?default=http://apidock.com/images/default_avatar_30.png&size=30)
Bangladeshi Taka (BDT 1,200.95)
Code example
def to_bdt(amount) number_to_currency(amount, :unit => "BDT ", :separator => ".", :delimiter => ",") end
![Default_avatar_30](https://www.gravatar.com/avatar/16b8d429e0123bc50ba8ad27cdaa6ff4?default=http://apidock.com/images/default_avatar_30.png&size=30)
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"