module DocTien

require “doc_tien/version”

Constants

A
B
C
D
E
SPECIAL
SPECIAL_2
SPECIAL_3
VERSION

Public Class Methods

now(s, char_currency='đồng') click to toggle source
# File lib/doc_tien.rb, line 24
def self.now(s, char_currency='đồng')
  s = s.to_s
   # Split every 3 number to 1 block
  s1 = s.reverse.scan(/.{1,3}/)
  s2 = s1.reverse.map do |d|
    d.reverse
  end
  # How many block
  num_of_blocks = s2.size
  # 5 is Trillion, 4 is Bilion, 3 is Milion, 2: Thousand, 1 Dong
  words_first = s2.map do |e|
    num_of_hundred(e.to_i)
  end
  words_string = num_to_words(words_first, num_of_blocks)
  words_string = replace_with_special_word(words_string)
  return words_string.lstrip.chop.capitalize+" "+char_currency
end
num_of_hundred(number) click to toggle source
# File lib/doc_tien.rb, line 54
def self.num_of_hundred(number)
  s = ""
  num_temp_mod100 = number%100
  if num_temp_mod100==0
    unless A[number/100]==''
      s = s+ A[number/100] + " "+ E.first
    end
    return s
  else
    #So hang tram
    tram = number/100
    if tram>0
        s =  s+ A[tram] +" "+ E.first
    end
    num_temp_mod10 = num_temp_mod100%10
    if (num_temp_mod10==0)
      #so hang chuc
      chuc = num_temp_mod100/10
      s = s+" "+ C[chuc*10]
      return s
    else
      chuc = num_temp_mod100/10
      if chuc>0
        s = s+" "+C[chuc*10]
      end
      donvi = num_temp_mod10
      s = s +" "+ A[donvi]
      if s.split(' ').size==3
        SPECIAL.each do |k, v|
          s.gsub!(k,v)
        end
      end
      return s
    end
  end
end
num_to_words(words_first, num_of_blocks) click to toggle source
# File lib/doc_tien.rb, line 42
def self.num_to_words(words_first, num_of_blocks)
  s = ""
  for i in 0...num_of_blocks
    if words_first[i]!=''
      s = s + words_first[i] + " "+ D[num_of_blocks-(i+1)]
      s = s +" "
    end

  end
  return s
end
replace_with_special_word(msg) click to toggle source
# File lib/doc_tien.rb, line 90
def self.replace_with_special_word(msg)
  input = msg
  SPECIAL_2.each do |k, v|
    input.gsub!(k,v)
  end
  SPECIAL_3.each do |k, v|
    input.gsub!(k,v)
  end
  return input
end