ruby 要旨
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 要旨相关的知识,希望对你有一定的参考价值。
#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)
#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)
以上是关于ruby 要旨的主要内容,如果未能解决你的问题,请参考以下文章