I am using the following method in a service (sms notification via. twilio), and I would like to add number_to_currency to the message so the amount is formatted nicely. I have tried a few approaches, but I cannot seem to get it to work. Any thoughts?
My method looks like this:
def message(details)
"A transaction of #{details[:amount]} was processed."
end
CodePudding user response:
Probably you can use NumberHelper#number_to_currency
include ActionView::Helpers::NumberHelper
def message(details)
"A transaction of #{number_to_currency(details[:amount])} was processed."
end
It will use current locale, but you can specify it and also format output with these options
:locale- Sets the locale to be used for formatting (defaults to current locale).
:precision- Sets the level of precision (defaults to 2).
:unit- Sets the denomination of the currency (defaults to “$”).
:separator- Sets the separator between the units (defaults to “.”).
:delimiter- Sets the thousands delimiter (defaults to “,”).
:format- Sets the format for non-negative numbers (defaults to “%u%n”). Fields are%ufor the currency, and%nfor the number.
:negative_format- Sets the format for negative numbers (defaults to prepending a hyphen to the formatted number given by:format). Accepts the same fields than:format, except%nis here the absolute value of the number.
:raise- If true, raisesInvalidNumberErrorwhen the argument is invalid.
:strip_insignificant_zeros- Iftrueremoves insignificant zeros after the decimal separator (defaults tofalse).
