2013-04-09 81 views
-1

我被困在數十億。錯誤是: 預計:「一十億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 
+0

請設置一個更好的問題標題。標題應該具有表現力,並快速總結這個問題的含義。幾乎所有問題都可以標題爲「幫助我」 – reto 2013-04-09 17:10:50

+0

,並請更正您代碼的縮進。 – reto 2013-04-09 17:12:15

回答

0

你的號碼範圍有一些很大的差距。看看他們,看看哪一個數字會落入567,890。答:沒有。就此而言,1,000,000或1,000,000,000也不會。它從99,999跳到10,000,001,然後從999,999,999跳到1,234,567,890。

+0

謝謝你veru多! ) 我固定thousends和所有工作正常。 – 2013-04-09 17:04:38

0

在你的範圍內取得一個高峯,他們似乎在10000之後到處都是。我會建議放權力,這樣你可以看到你期望的範圍更容易。例如百萬分之一範圍是(10000001..999999999),應該是 (10**6...10**9)