Created
February 17, 2014 07:03
-
-
Save carolineartz/fd9f268c9271f62c410c to your computer and use it in GitHub Desktop.
gist
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
| #in_words(821_713) # => "eight hundred twenty one thousand seven hundred thirteen" | |
| #in_words(34_567_893) # => "thirty four million five hundred sixty seven thousand eight hundred ninety three" | |
| # pseudocode | |
| # | |
| class NumbersWords | |
| WORD_LOOKUP = {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'} | |
| MAGNITUDE = {3 => 'hundred', 4 => 'thousand', 6 => 'million', 9 => 'billion'} | |
| def initialize(number) | |
| @number = number | |
| end | |
| def in_words(number) | |
| WORD_LOOKUP.fetch(number) do |num| | |
| digit_array = num.to_s.chars.map {|char| char.to_i} | |
| "#{WORD_LOOKUP[digit_array[0]*10]} #{WORD_LOOKUP[digit_array[1]]}" | |
| end | |
| end | |
| def in_words_thousand | |
| something = to_digit_array(@number) | |
| while something.length > 2 | |
| print "#{in_words(something.shift)} hundred " | |
| end | |
| @number = something.join.to_i | |
| print self.in_words(@number) | |
| end | |
| def to_digit_array(number) | |
| @number.to_s.chars.map {|char| char.to_i} | |
| end | |
| end | |
| converter = NumbersWords.new(663) | |
| converter.in_words_thousand | |
| #p in_words(29) | |
| ##p in_words(100) | |
| #p in_words(1) | |
| #p in_words(4) | |
| #p in_words(27) | |
| #p in_words(92) | |
| #p in_words(345) | |
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
| #in_words(821_713) # => "eight hundred twenty one thousand seven hundred thirteen" | |
| #in_words(34_567_893) # => "thirty four million five hundred sixty seven thousand eight hundred ninety three" | |
| # pseudocode | |
| # | |
| class NumbersWords | |
| WORD_LOOKUP = {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'} | |
| MAGNITUDE = {3 => 'hundred', 4 => 'thousand', 6 => 'million', 9 => 'billion'} | |
| #def initialize(number) | |
| # @number = number | |
| # in_words_thousand | |
| #end | |
| def in_words(number) | |
| WORD_LOOKUP.fetch(number) do |num| | |
| digit_array = to_digit_array(num) | |
| "#{WORD_LOOKUP[digit_array[0]*10]} #{WORD_LOOKUP[digit_array[1]]}" | |
| end | |
| end | |
| def in_words_thousand(number) | |
| digit_array = to_digit_array(number) | |
| while digit_array.length > 2 | |
| print "#{in_words(digit_array.shift)} hundred " | |
| end | |
| number = digit_array.join.to_i | |
| print self.in_words(number) | |
| end | |
| def to_digit_array(number) | |
| number.to_s.chars.map {|char| char.to_i} | |
| end | |
| end | |
| converter = NumbersWords.new | |
| puts converter.in_words_thousand(76) | |
| puts converter.in_words_thousand(334) | |
| puts converter.in_words_thousand(500) | |
| #converter.in_words_thousand | |
| #p in_words(29) | |
| ##p in_words(100) | |
| #p in_words(1) | |
| #p in_words(4) | |
| #p in_words(27) | |
| #p in_words(92) | |
| #p in_words(345) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment