Commit 69b8ea40 by Philipp Hansch

### Refactoring

parent e507f3fa
 module Inwords def in_words self.to_word end #where the magic happens def to_word return "zero" if self.zero? string = "" number = self number, string = large_numbers(self) string << small_numbers(number) end # Converts numbers > 99 def large_numbers(number) string = "" orders = [ [100, "hundred"], [1000, "thousand"], ... ... @@ -21,32 +20,35 @@ module Inwords orders.reverse.each do |limit, name| if number > limit - 1 quotient, rest = number.divmod(limit) number = rest if quotient > 0 string << "#{quotient.to_word} #{name}" number = rest if number > 0 string << " " end string << "#{quotient.in_words} #{name}" string << " " if number > 0 end end end return number, string end # Converts the numbers 1 - 99 def small_numbers(number) string = "" ones = %W(one two three four five six seven eight nine) teens = %W(ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen) tens = %W(ten twenty thirty forty fifty sixty seventy eighty ninety) tens = %W(twenty thirty forty fifty sixty seventy eighty ninety) if number < 10 && number > 0 string += ones[number-1] elsif number < 20 && number > 0 string += teens[number-10] elsif number < 100 && number > 0 if number > 19 # 20 - 99 quotient, rest = number.divmod(10) string_tens = "" ten_part = quotient * 10 string_tens += ones[rest-1] if rest != 0 string_tens.prepend(tens[ten_part/10 - 1] + " ") string_tens.strip! if rest == 0 string += string_tens string << tens[quotient - 2] if rest != 0 string << " " string << ones[rest - 1] end elsif number > 9 # 10 - 19 string << teens[number - 10] elsif number > 0 # 1 - 9 string << ones[number - 1] end string ... ... @@ -59,5 +61,4 @@ end class Bignum include Inwords end end \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!