Class: Formatters::FormatCurrency
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/formatters/format_currency.rb
Instance Method Summary collapse
- #from(value, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ FormatCurrency
constructor
A new instance of FormatCurrency.
- #to(str, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ FormatCurrency
8 9 10 |
# File 'lib/formatters/format_currency.rb', line 8 def initialize( = {}) @precision = [:precision] || 2 end |
Instance Method Details
#from(value, options = {}) ⇒ Object
12 13 14 15 |
# File 'lib/formatters/format_currency.rb', line 12 def from(value, = {}) = {:precision => @precision}.merge() number_to_currency(value.to_f / (10 ** @precision), ) end |
#to(str, options = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/formatters/format_currency.rb', line 17 def to(str, = {}) return nil if str.nil? or str.empty? val = str.gsub(/[^0-9.\-]/, '').to_f (val * (10 ** @precision)) unless val.nil? end |