Class: FarsiFu::NumToWord

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

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ NumToWord

Returns a new instance of NumToWord.



6
7
8
# File 'lib/farsifu/num_to_word.rb', line 6

def initialize(number)
  @number = number
end

Instance Method Details

#spell_farsiObject

Spells a number in Persian accpets english numbers (in float,fixnum or string)

Example:

5678.spell_farsi # => "پنج هزار و ششصد و هفتاد و هشت"


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/farsifu/num_to_word.rb', line 15

def spell_farsi
  # Distigushing the number (float and )
  if @number.class == Float
    num_array = @number.to_f.to_s.split(".").first.split(//).reverse
    dec_array = @number.to_f.to_s.split(".").last.split(//).slice(0..9).compact.reverse
    dec_copy_b = dec_array.clone ; dec_copy_a = dec_array.clone
    result = spell(num_array)
    ( result += PERSIAN_DIGIT_SIGN[2] + spell(dec_array) + " " + PERSIAN_DIGIT_SPELL["decimals"][dec_copy_a.size].to_s )  unless [PERSIAN_DIGIT_SPELL[0][10],""].include? spell(dec_copy_b)
    return result
  else
    num_array = @number.to_i.to_s.split(//).reverse
    return spell(num_array)
  end
end

#spell_ordinal_farsi(*args) ⇒ Object

Spells numbers in sequentional format. If pass ‘true`, it will use the second format

Example:

1.spell_ordinal_farsi       # => "اول"
121.spell_ordinal_farsi     # => "صد و بیست و یکم"
2.spell_ordinal_farsi(true) # => "دومین"
2054.spell_ordinal_farsi(true) # => "دو هزار و پنجاه چهارمین"


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/farsifu/num_to_word.rb', line 37

def spell_ordinal_farsi(*args)
  if args[0]
    exceptions = {0 => "صفر", 1 => "اولین", 3 => "سومین"}
    suffix = "مین"
  else
    exceptions = {0 => "صفر", 1 => "اول", 3 => "سوم"}
    suffix = "م"
  end

  make_ordinal_spell(exceptions, suffix)
end