tomislav-b
8/27/2013 - 7:10 PM

Given an integer in the range of 0 < X < 10**26 returns the string representation of the given number. Like: 25161045656 => 'twenty five bil

Given an integer in the range of 0 < X < 10**26 returns the string representation of the given number. Like: 25161045656 => 'twenty five billion one hundred sixty one million forty five thousand six hundred fifty six'

$numbers = {0 => "", 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",
                        20 => "twenty", 30 => "thirty", 40 => "forty",
                        50 => "fifty", 60 => "sixty",
                        70 => "seventy", 80 => "eighty",
                        90 => "ninety", 10**2 => "hundred"}

$large_numbers = {1 => "", 10**3 => "thousand", 10**6 => "million",
                   10**9 => "billion", 10**12 => "trillion",
                   10**15 => "quadrillion", 10**18 => "quintillion",
                   10**21 => "sextillion", 10**24 => "septillion"}


def small_number_to_word(number)
  english = ""

  if number.abs.to_s.length > 2
    english << $numbers[(number % 1000 - number % 100) / 100] + " " + $numbers[100] + " "
  end

  if $numbers.keys.include?(number % 100) and number != 100
    english << $numbers[number % 100]
    return english.strip
  end

  english += $numbers[number % 100 - number % 10] + " " + $numbers[number % 10] + " "

  return english.strip
end

def int_to_english(number)
  english = ""
  large_num = 1
  temp_list = []

  while number != 0
    temp_list << $large_numbers[large_num] + " "
    temp_list << small_number_to_word(number % 1000) + " "

    number = number / 1000
    large_num = large_num * 1000
  end

  temp_list.reverse!
  temp_list.each do |string|
    english << string
  end

  return english.strip
end
numStrList = {0: "", 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",
                        20: "twenty", 30: "thirty", 40: "forty",
                        50: "fifty", 60: "sixty",
                        70: "seventy", 80: "eighty",
                        90: "ninety", 10**2: "hundred"}

largeNumStrList = {1: "", 10**3: "thousand", 10**6: "million",
                   10**9: "billion", 10**12: "trillion",
                   10**15: "quadrillion", 10**18: "quintillion",
                   10**21: "hexillion", 10**24: "heptillion"}


def smallNumberToWord(number):
    """Returns the string representation of a given integer
    input: 0 < X < 1000"""

    numWord = ""

    if len(str(abs(number))) > 2:
        numWord += numStrList[(number % 1000 - number % 100) / 100] + " " + \
            numStrList[100] + " "

    if number % 100 in numStrList.keys() and number != 100:
        numWord += numStrList[number % 100]
        return numWord.strip()

    numWord += numStrList[number % 100 - number % 10] + " " + \
        numStrList[number % 10] + " "

    return numWord.strip()


def numberToWord(number):
    """
    Returns the string representation of a given integer
    input: int 0 < X < 10**24
    """
    numWord = ""
    largeNum = 1
    tempList = []

    while number != 0:
        tempList.append(largeNumStrList[largeNum] + " ")
        tempList.append(smallNumberToWord(number % 1000) + " ")

        number /= 1000
        largeNum *= 1000

    tempList.reverse()
    for string in tempList:
        numWord += string

    return numWord.strip()

# MAIN function
if __name__ == '__main__':
    print numberToWord(25161045656)