Module: FarsiFu::NumbersExtensions::InstanceMethods
- Defined in:
- lib/farsifu.rb
Constant Summary collapse
- PERSIAN_CHARS =
"۱۲۳۴۵۶۷۸۹۰،؛"
- ENGLISH_CHARS =
"1234567890,;"
- PERSIAN_DIGIT_JOINT =
" و "
- PERSIAN_DIGIT_SIGN =
["منفی ", "مثبت ", " ممیز "]
- PERSIAN_DIGIT_SPELL =
{ 0 => [ nil ,"یک","دو","سه","چهار","پنج","شش","هفت","هشت","نه", "صفر"] , 1 => [ nil ,"ده","بیست","سی","چهل","پنجاه","شصت","هفتاد","هشتاد","نود"], 2 => [ nil ,"صد","دویست","سیصد","چهارصد","پانصد","ششصد","هفتصد","هشتصد","نهصد"], "10..19" => [ "ده" ,"یازده","دوازده","سیزده","چهارده","پانزده","شانزده","هفده","هجده","نوزده"], "zillion" => [ nil ,"هزار","میلیون","میلیارد","بیلیون","تریلیون","کوادریلیون","کوینتیلیون","سیکستیلیون","سپتیلیون","اکتیلیون","نونیلیون","دسیلیون"], "decimals" => [ nil, "دهم", "صدم", "هزارم", "دههزارم", "صدهزارم", "میلیونیم", "دهمیلیونیم","صدمیلیونیم","میلیاردیم"] }
Instance Method Summary collapse
-
#spell_farsi ⇒ Object
Spells a number in Persian accpets english numbers (in float,fixnum or string).
-
#spell_ordinal_farsi(*args) ⇒ Object
Spells numbers in sequentional format.
-
#to_english ⇒ Object
Returns a string which is the equivalent English number of a Persian number (in String).
-
#to_farsi ⇒ Object
(also: #to_persian)
Returns a string which is the equivalent Persian number of an English number (in String) accepts instances of String, Integer and Numeric classes (Fixnum,Bignum and floats are accepted).
Instance Method Details
#spell_farsi ⇒ Object
Spells a number in Persian accpets english numbers (in float,fixnum or string)
Example:
5678.spell_farsi # => "پنج هزار و ششصد و هفتاد و هشت"
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/farsifu.rb', line 54 def spell_farsi # Distigushing the number (float and ) if self.class == Float num_array = self.to_f.to_s.split(".").first.split(//).reverse dec_array = self.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 = self.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) # => "دو هزار و پنجاه چهارمین"
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/farsifu.rb', line 76 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 |
#to_english ⇒ Object
Returns a string which is the equivalent English number of a Persian number (in String)
Example:
"۱۲۳".to_english # => "123"
31 32 33 |
# File 'lib/farsifu.rb', line 31 def to_english self.to_s.tr(PERSIAN_CHARS,ENGLISH_CHARS) end |
#to_farsi ⇒ Object Also known as: to_persian
Returns a string which is the equivalent Persian number of an English number (in String) accepts instances of String, Integer and Numeric classes (Fixnum,Bignum and floats are accepted)
alias: to_persian
Example:
"123".to_farsi # => "۱۲۳"
"456".to_persian # => "۴۵۶"
789.to_farsi # => "۷۸۹"
44 45 46 |
# File 'lib/farsifu.rb', line 44 def to_farsi self.to_s.tr(ENGLISH_CHARS,PERSIAN_CHARS) end |