Created
June 2, 2012 05:31
-
-
Save authorNari/2856773 to your computer and use it in GitHub Desktop.
English Numerals by @Nari3 , @Watson1978 #minatork01
This file contains 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
require_relative "ex_numeric" | |
puts ARGV.shift.to_i.to_english |
This file contains 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
class Numeric | |
def to_english | |
return NumToEnglish.new.convert(self) | |
end | |
end | |
class NumToEnglish | |
def initialize | |
@num_to_english = [] | |
build_num_to_english | |
end | |
def convert(num) | |
@num_to_english[num] | |
end | |
private | |
def build_num_to_english | |
build_0_to_19 | |
build_20_to_99 | |
build_100_to_9999 | |
end | |
def build_0_to_19 | |
zero_to_nineteen = %W(zero one two three four five six | |
seven eight nine ten eleven twelve thirteen | |
fourteen fifteen sixteen seventeen eighteen nineteen) | |
@num_to_english.concat(zero_to_nineteen) | |
end | |
def build_20_to_99 | |
twenty_to_ninety = %W(twenty thirty forty fifty sixty seventy eighty ninety) | |
res = twenty_to_ninety.each_with_object([]) do |i, r| | |
r << "#{i}" | |
(1..9).each do |j| | |
r << "#{i} #{@num_to_english[j]}" | |
end | |
end | |
@num_to_english.concat(res) | |
end | |
def build_100_to_9999 | |
(100..9999).map{|i| [(i / 100), (i % 100)] }.each do |i, j| | |
if j == 0 | |
if (i % 10) == 0 | |
@num_to_english << @num_to_english[i] + " thousand" | |
else | |
@num_to_english << @num_to_english[i] + " houndred" | |
end | |
next | |
end | |
if (i % 10) == 0 | |
@num_to_english << @num_to_english[i] + " thousand and " + @num_to_english[j] | |
else | |
@num_to_english << @num_to_english[i] + " houndred and " + @num_to_english[j] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment