Last active
December 25, 2015 12:09
-
-
Save bjhaid/6974779 to your computer and use it in GitHub Desktop.
Conversion of arabic to english words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ONE_TO_NINETEEN = { 1=> "one", 2=> "two", 3=> "three", 4=> "four", 5=> "five", 6=> "six", 7=> "seven", 8=> "eight", 9=> "nine", 10=> "ten", 11=> "eleven", 12=> "twelve", 13=> "thirteen", 14=> "fourteen", 15=> "fifteen", 16=> "sixteen", 17=> "seventeen", 18=> "eighteen", 19=> "nineteen" } | |
TENS = {20=>"twenty", 30=>"thirty", 40=>"forty", 50=>"fifty", 60=>"sixty", 70=>"seventy", 80=>"eighty", 90=>"ninety"} | |
MULTIPLES = {100 => "hundred", 1000 => "thousand", 1000000 => "million", 1000000000 => "billion", 1000000000000 => "trillion" } #extending the numerals it can convert to words can be done by appending the key value pair to the MULTIPLES | |
def arabic2words_under_100(num) | |
word = "" | |
while num > 19 | |
divisor_num = (num / 10) | |
num = (num % 10) | |
(word << TENS[divisor_num * 10]) unless TENS[divisor_num * 10].nil? | |
end | |
if !word.empty? | |
(word << " #{ONE_TO_NINETEEN[num]}") unless ONE_TO_NINETEEN[num].nil? | |
else | |
(word << "#{ONE_TO_NINETEEN[num]}") unless ONE_TO_NINETEEN[num].nil? | |
end | |
return nil if word.empty? | |
word | |
end | |
def arabic2words(num) | |
word = "" | |
while num >= 100 | |
MULTIPLES.keys.sort.reverse.each do |multiple| | |
divisor_num = (num / multiple) | |
next if divisor_num < 1 | |
if arabic2words(divisor_num).empty? | |
pre = arabic2words_under_100(divisor_num) | |
else | |
pre = arabic2words(divisor_num) | |
end | |
num = (num % multiple) | |
word = "#{word}, " unless word.empty? | |
word << "#{pre} " | |
word << MULTIPLES[multiple] | |
end | |
end | |
word << " and #{arabic2words_under_100(num)}" unless arabic2words_under_100(num).nil? | |
word.gsub(/^ and /,"") | |
end | |
arabic2words 123030323 # => "one hundred and twenty three million, thirty thousand, three hundred and twenty three" | |
arabic2words 13 # => "thirteen" | |
arabic2words 133 # => "one hundred and thirty three" | |
arabic2words 164 # => "one hundred and sixty four" | |
arabic2words 1023 # => "one thousand and twenty three" | |
arabic2words 11000 # => "eleven thousand" | |
arabic2words 210001 # => "two hundred and ten thousand and one" | |
arabic2words 1000111352011 # => "one trillion, one hundred and eleven million, three hundred and fifty two thousand and eleven" | |
arabic2words 1111352011 # => "one billion, one hundred and eleven million, three hundred and fifty two thousand and eleven" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment