Module: SmartCurrency::InstanceMethods

Defined in:
lib/smart_currency.rb

Overview

any method placed here will apply to instaces, like @hickwall

Instance Method Summary collapse

Instance Method Details

#currency_to_db(string) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/smart_currency.rb', line 36

def currency_to_db(string)
  if string.is_a?(String) 
    return (string.gsub(/\./, '').gsub(/,/, '.')).to_f
  elsif string.nil?
    return 0
  else  
    return string
  end      
end

#currency_to_view(decimal) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smart_currency.rb', line 46

def currency_to_view(decimal)
  if !decimal.nil?
    str = decimal.to_s.split(".")
    str[1] = "0" unless str[1]
    if str[1].size == 1
      str[1] += "0"
    end
    if str[0].size == 5 || str[0].size == 6
      str[0].gsub!(/\B([0-9]{3})\b/,'.\1')
    elsif str[0].size > 7
      str[0].gsub!(/([0-9]{3})\b/,'.\1').gsub!(/([0-9]{3}\.)/, '.\1') 
    else
      str[0].gsub!(/\B([0-9]{3})/,'.\1')  
    end
    str[1].size > 2 ? str[1] = str[1][0..1] : nil
    return (str[0] + "," + str[1]).to_s  
  else                                 
    return "keine Angabe"
  end  
end