Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/spanish_number.rb

Instance Method Summary collapse

Instance Method Details

#mxn(cents) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/spanish_number.rb', line 51

def mxn cents
  text = ""
  text += " pesos"
  cents = "00" if cents.nil?
  while cents.length < 2 do
    cents << "0"
  end
  text += " " + cents[0..1].to_s + "/100"
  text += " M.N."
end

#solve_million(sixdigit) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/spanish_number.rb', line 25

def solve_million sixdigit
  text = ""
  text += solve_thousand sixdigit.first
  if sixdigit.length > 1
    text += " mil"
    text += solve_thousand sixdigit.last
  end
  text
end

#solve_thousand(threedigit) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spanish_number.rb', line 35

def solve_thousand threedigit
  text = ""
  return "error" if threedigit.length > 3
  text += " " + CENTENAS[ threedigit[2].to_i ] if threedigit.length > 2 && threedigit[2] != '0'
  if threedigit.length >= 2 && threedigit[1].ord > '2'.ord
    text += " " + DECENAS[ threedigit[1].to_i ] if threedigit[1] != '0'
    unit = threedigit[0].to_i
    text += " y" if unit != 0
    text += " " + UNIDADES[ unit ] if unit != 0
  else
    unit = threedigit[0..1].reverse.to_i
    text += " " + UNIDADES[ unit ] if unit != 0
  end
  text
end

#to_spanish_text(currency = "") ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spanish_number.rb', line 7

def to_spanish_text currency=""
  final_text = ""
  self.to_s =~ /([^\.]*)(\..*)?/
  int, dec = $1.reverse, $2 ? $2[1..-1] : ""
  int = int.scan(/.{1,6}/).reverse
  int = int.map{ |million| million.scan(/.{1,3}/).reverse}
  int.each_with_index do |sixdigit, index|
    i = int.length - index
    final_text += solve_million sixdigit
    if (i-2) >= 0
      final_text += " " + MILLONES[ i-2 ]
      final_text += "es" if sixdigit != ["1"]
    end
  end
  final_text += mxn dec if currency == 'mxn'
  final_text
end