请帮我修复错误

时间:2013-04-09 16:45:51

标签: ruby

我被困在数十亿。错误是: 预期:“十亿二百三十四百万五百六十七数千八百九十” 得到:“十亿二百三十四万”(使用==)

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

你的号码范围有一些很大的差距。看看它们,看看567,890中的哪一个会落入其中。答:没有。就此而言,1,000,000或1,000,000,000也不会。它从99,999跳到10,000,001,然后从999,999,999跳到1,234,567,890。

答案 1 :(得分:0)

在你的范围内达到顶峰,它们似乎在10,000之后到处都是。我会建议使用权力,这样你就可以更容易地看到你期望的范围。例如,百万范围是(10000001..999999999),应该是。{ (10**6...10**9)