-1

我被困在数十亿美元。错误是: 预期:“12.34567890亿” 得到: “12.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
4

2 回答 2

0

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

于 2013-04-09T16:59:48.630 回答
0

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

于 2013-04-09T17:05:59.223 回答