Rails number_to_currency change locale defaults
ActiveSupport number_to_currency
helper method may be extremely useful if you deal with prices/wages inside your app. By default number_to_currency
uses your current locale default settings which are OK in most cases but sometimes you may want to change them.
In my case, I have to always display trailing zeros for each price. By default number_to_currency
doesn't display them at least for a locale which I was using.
I found an option called strip_insignificant_zeros
, which is/was not documented in the official number_to_currency
docs. Setting it to true
for each number_to_currency
execution seems like a bad idea (not DRY at all).
Fortunately, you may define global locale default options inside your e.g. config/locales/en.yml like so:
en:
number:
currency:
format:
strip_insignificant_zeros: false
The same format might be used for other number_to_currency
options e.g.
en:
number:
currency:
format:
delimiter: " "
format: "%n %u"
precision: 2
separator: ","
strip_insignificant_zeros: false
unit: "PLN"
One more hint about number_to_currency
usage is using:
ActiveSupport::NumberHelper.number_to_currency(123.12)
if you need to use it outside the View.