Module: SmartCurrency::InstanceMethods

Defined in:
lib/smart_currency.rb

Instance Method Summary collapse

Instance Method Details

#currency_to_db(string) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/smart_currency.rb', line 40

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/smart_currency.rb', line 50

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

    return (str[0] + "," + str[1]).to_s
  else
    return "keine Angabe"
  end
end