我被困在數十億。錯誤是: 預計:「一十億2.34億567890」 有:「一十億2.34億」(使用==)請幫我修復這個錯誤
這裏是我的代碼:
class Fixnum
def in_words
less_than_13={0 => 'zero', 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'}
tens={20 => 'twenty', 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'}
case self
when 0..13
return less_than_13[self]
when 14, 16, 17, 19
return teenify
when 15
return 'fifteen'
when 18
return 'eighteen'
when 20, 30, 40, 50 , 60, 70, 80, 90
return tens[self]
when (20..99)
# tens = (self/10) * 10
# tens = (77/10) * 10
# tens = (7) * 10
# tens = 70
tens = (self/10) * 10
# ones = self - tens
# ones = 77 - 70
# ones = 7
ones = self - tens
return "#{tens.in_words} #{ones.in_words}"
when (100..999)
# 100
hundreds = self/100
rest = self - (hundreds * 100)
if rest > 0
return "#{hundreds.in_words} hundred #{rest.in_words}"
else
return "#{hundreds.in_words} hundred"
end
when (999..99999)
thousend = self/1000
rest = self - (thousend * 1000)
if rest > 0
return "#{thousend.in_words} thousand #{rest.in_words}"
else
return "#{thousend.in_words} thousand"
end
when (10000001..999999999)
million = self/1000000
rest = self - (million * 1000000)
if rest > 0
return "#{million.in_words} million #{rest.in_words}"
else
return "#{million.in_words} million"
end
when (1234567890..999999999999)
billion = self/1000000000
rest = self - (billion * 1000000000)
if rest > 0
return "#{billion.in_words} billion #{rest.in_words}"
else
return "#{billion.in_words} billion"
end
end
end
def teenify
return (self - 10).in_words + 'teen'
end
end
請設置一個更好的問題標題。標題應該具有表現力,並快速總結這個問題的含義。幾乎所有問題都可以標題爲「幫助我」 – reto 2013-04-09 17:10:50
,並請更正您代碼的縮進。 – reto 2013-04-09 17:12:15